Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > comparing portion of string to another string

Reply
Thread Tools

comparing portion of string to another string

 
 
Owner
Guest
Posts: n/a
 
      02-18-2011
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.
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      02-18-2011
Owner <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Owner
Guest
Posts: n/a
 
      02-18-2011
On Fri, 18 Feb 2011 01:41:58 +0000, Jens Thoms Toerring wrote:

> Owner <(E-Mail Removed)> wrote:
>> 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!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Border for only portion of a cell? VB Programmer ASP .Net 2 06-10-2004 12:27 PM
Please help to extract the decimal portion Tharma Perl 5 05-21-2004 09:40 AM
Writing a large string buffer to file cuts off portion Arun Kumar Srinivasan Java 2 01-29-2004 05:06 PM
removing the time portion of date field Serge Poirier ASP .Net 1 11-08-2003 08:26 PM
String question: Returning portion of string with words surrounding highlighted search term? Ken Fine ASP General 4 08-14-2003 06:04 AM



Advertisments