[OCLUG-devel] comparing strings

Christopher Smith x at xman.org
Fri Sep 2 15:25:40 PDT 2005


James Colannino wrote:
> Hey everyone.  I have a quick question.  Let's say that I have a single
> string for reference with the following contents:
> 
> "stringone\nstringtwo\nstringthree\n"
> 
> What I want to do is compare the contents of one string with each
> "element" in the above string (each string I want to check against is
> separated by \n's.)  So first I would compare a string to "stringone,"
> then compare it to "stringtwo," etc.  What's a good way for me to do
> this in C?

Assuming the string you are searching with doesn't have any carriage
returns in it, why not just use strstr()? It'll return the first place
in the larger string that has a match, and you assume all preceding ones
aren't a match. You can then invoke it starting at the next address
after the one returned by strstr(). Repeat until NULL.

--Chris


More information about the OCLUG-devel mailing list