How do I locate a specific character within a string? I know that there is probably a simple answer but I am a student who is just learning.
Printable View
How do I locate a specific character within a string? I know that there is probably a simple answer but I am a student who is just learning.
I'd implement it in a function, it's much more elegant, and has the advantage of returning every occurence of the char or substring.
Scan the string from it's first index until you find your character, then return it's index (you can even store it, then continue the search for another occurence).
If you reached the end, and no char found, the function should return -1.
The strstr() function will return the first occurance of a character or substring within another string.
I use strchr() for this....
Is there any chance of an example of how to use this function?
I am only a newbie to all this as well :(
char test[] = "hello world";
char *p = strchr( test, 'w' );
// p now points at the 'w' in world