comparing portion of string to another string

O

Owner

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.
 
J

Jens Thoms Toerring

Owner said:
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.

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
 
O

Owner

Owner said:
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.

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top