Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:44:21+00:00 2026-06-12T22:44:21+00:00

After going through a bunch of threads, I know that I need to use

  • 0

After going through a bunch of threads, I know that I need to use regex.h for using regular expressions in C & C++.

But I was wondering, is there an easier way to search for occurrence of “/” or “\” in a string.

// I have a strstr statement like this -
str = strstr(s, "/");

I was wondering if it is possible to change it so that I can search for the first occurrence of a
/
or \ in a single call to strstr.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:44:22+00:00Added an answer on June 12, 2026 at 10:44 pm

    Try strcspn:

    Get span until character in string
    Scans str1 for the first occurrence of any of the characters that are part of str2, returning the number of characters of str1 read before this first occurrence.

    The search includes the terminating null-characters. Therefore, the function will return the length of str1 if none of the characters of str2 are found in str1.

    Example:

    #include <stdio.h>
    #include <string.h>
    
    const char* findany(const char* s, const char* keys)
    {
        const char* tmp;
        tmp = s + strcspn(s,keys);
        return *tmp == '\0' ? NULL : tmp;
    }
    
    int main ()
    {
      char str1[] = "abc\\123";
      char str2[] = "abc/123";
      char str3[] = "abc123";
      char keys[] = "/\\";
      printf("1: %s\n",findany(str1,keys));
      printf("2: %s\n",findany(str2,keys));
      printf("3: %s\n",findany(str3,keys));
      return 0;
    }
    

    Edit: strpbrk does the same thing as findany above. Didn’t see that function:

    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char str1[] = "abc\\123";
      char str2[] = "abc/123";
      char str3[] = "abc123";
      char keys[] = "/\\";
      printf("1: %s\n",strpbrk(str1,keys));
      printf("2: %s\n",strpbrk(str2,keys));
      printf("3: %s\n",strpbrk(str3,keys));
      return 0;
    }
    

    Output (of both):

    1: \123
    2: /123
    3: (null)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After going through couple of linksI came to know that Perl does the compilation
After going through every possible resources I came to conclusion that I need an
After going through Marshall code snippet Got the Idea that marshalling is used to
I have planed to start using a framework in php development. After going through
After going through some examples on the web I realize that there is a
Please pardon me if I am asking an obvious question, but after going through
After going through some text and source code i realized that fork , vfork
After going through the article at http://geekexplains.blogspot.com/2008/06/diff-between-externalizable-and.html , i got to know Externalizable is
I need to load the higher resolution image in UIImageView. After going through approaches
I am planning to study on gwt but after going through some introductions, it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.