comparing portion of string to another string

Discussion in 'C Programming' started by Owner, Feb 18, 2011.

  1. Owner

    Owner Guest

    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.
     
    Owner, Feb 18, 2011
    #1
    1. Advertising

  2. Owner <> 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://toerring.de
     
    Jens Thoms Toerring, Feb 18, 2011
    #2
    1. Advertising

  3. Owner

    Owner Guest

    On Fri, 18 Feb 2011 01:41:58 +0000, Jens Thoms Toerring wrote:

    > Owner <> 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!!
     
    Owner, Feb 18, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tharma
    Replies:
    5
    Views:
    541
    Skeleton Man
    May 21, 2004
  2. Serge Poirier

    removing the time portion of date field

    Serge Poirier, Nov 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    485
    Lyndon Hills
    Nov 8, 2003
  3. VB Programmer

    Border for only portion of a cell?

    VB Programmer, Jun 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    334
    VB Programmer
    Jun 10, 2004
  4. Arun Kumar Srinivasan
    Replies:
    2
    Views:
    11,486
    Gregory A. Swarthout
    Jan 29, 2004
  5. Ken Fine
    Replies:
    4
    Views:
    214
    Ken Fine
    Aug 14, 2003
Loading...

Share This Page