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.
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.
Last edited by Carlos; 10-20-2001 at 02:03 AM.
[R]evolution!
Programming related articles, downloads, demos
The strstr() function will return the first occurance of a character or substring within another string.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
I use strchr() for this....
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.