comparing portion of string to another string

comparing portion of string to another string

Post by Owne » Sat, 19 Feb 2011 10:27:11


How do I compare portion of string to another string?

for example

i'd like to do this

char s1[100], s2[100];
int i = strlen(s1);

strcmp(s1[i-3],s2)
^
passing only portion of the string to compare.
 
 
 

comparing portion of string to another string

Post by jt » Sat, 19 Feb 2011 10:41:58


A bit more of context would be helpful. Perhaps what you
are looking for is

strcmp( s1 + i - 3, s2 );

i.e. you pass a pointer to the 'i-3'th element to strcmp(),
or, written differently,

strcmp( &s1[ i - 3 ], s2 );

For other cases there's also strncmp() where you can tell
how many chars you want to have compared (at most) before
the function decided what to return.

Regards, Jens
--
\ Jens Thoms Toerring ___ XXXX@XXXXX.COM
\__________________________ http://www.yqcomputer.com/

 
 
 

comparing portion of string to another string

Post by Owne » Sat, 19 Feb 2011 10:46:43


thank you strcmp( &s1[ i - 3 ], s2 ); worked!!