How to find the first occurrence of one of several characters (other than using regex)
An answer to this question on Stack Overflow.
Question
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.
Answer
You could try using the strchr function to find the first occurrence of a character:
pos_of_first_slash=strchr(s, '/');
Returns a pointer to the first occurrence of character in the C string str.
The terminating null-character is considered part of the C string. Therefore, it can also be located in order to retrieve a pointer to the end of a string.
Returns a pointer to the first occurrence of character in str. If the character is not found, the function returns a null pointer.