For example: If you want to check whether a string has a '@' in it. If not, then ask the user to input another string.
For example: If you want to check whether a string has a '@' in it. If not, then ask the user to input another string.
You can make a function which loops through a string until '@' is found.
Don't forget to have a maximum string length entered into the function
Fact - Beethoven wrote his first symphony in C
Good call, Tclausex
I'd recommend strnchr though
Fact - Beethoven wrote his first symphony in C
I don't think I do any more, because I've just discovered that it is a non-standard function!
However, the reason I did recommend it was because if no NUL character on the end of the string (for whatever reason) and the character is not found, the function will not return a pointer to non-valid memory.
strchr will keep looking until if finds a match -> It will keep going outside the string.
Here is an example that although is not very realistic, and I'd imagine that it is more of an issue with non-hosted environments where variables are packed together.
Code:/* Note that the error has been inserted intentionally in a unrealistic situation. */ int main(void) { const char s[5] = "abcde"; const char t[] = "fghij"; char *p; p = strchr(s, 'f'); if (p) { puts(p); } return 0; }
Fact - Beethoven wrote his first symphony in C
memchr is better for that, since strnchr will waste cycles checking for the null character on every iteration. I think it's only necessary in the unusual situation where you want to search, for example, four characters of a string without knowing the length of the string in advance. That way it won't go past the null character if the string has less than four characters, and it won't search past the fourth character if the string has more than four characters.
I think that the real danger is strchr returning a pointer to a location which is not in the buffer because the NUL character is missing.
memchr is probably the way to go.
Fact - Beethoven wrote his first symphony in C