• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Searching a string in C

Isocene

Senior member
If I have a string that is an email address (blah@place.com) how would I chop it into (place.com) in C?

I used

strcpy(tmp, strchr(tempadr,'@'));

but it gives me @place.com and I dont know how to omit just the first character. Anyone have any ideas?

Mike
 
Nevermind... the solution I posted was far too quickly thought out, and won't work. 🙂

Or, actually, it probably will work. 🙂

strchr will return the pointer to the first character you're searching for, in this case @, so just add 1 to it.

This should work, and is what I posted earlier:

strcpy(tmp, strchr(tempadr,'@')+1);

It's been a long day.... 🙂
 
strchr() + 1 will do it. But I wouldn't recommend it unless you're 100% sure your input string is well formed (and you've also done the proper string-length checks).
 
Personally, I would use a regular expression for this. There are quality regex implementation libraries available, including the one that comes with GNU libc. I believe there is a win32 port for it as well.

[edit]Just to expound... the reason I'd use a regex is for validation; simply parsing it is easy enough.[/edit]
 
Originally posted by: singh
strchr() + 1 will do it. But I wouldn't recommend it unless you're 100% sure your input string is well formed (and you've also done the proper string-length checks).

Easy to tell.

strchr will return NULL if it finds the end of the string (determined by '\0' char which all your strings should end with) before it finds a '@'.
 
strchr will return NULL if it finds the end of the string (determined by '\0' char which all your strings should end with) before it finds a '@'.

Yes it's easy to tell, but the posted code "strcpy(tmp, strchr(tempadr,'@')+1);" doesn't even make an attempt 😉

Bill


 
Ya, I know... I felt a little bad for suggesting something that might be a tad unsafe...

It was a quick post just to get him going with whatever he was doing... in the future I'll take a bit more time to explain. 🙂

 
Back
Top