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

  2. 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, Feb 18, 2011
    #2
    1. Advertisements

  3. Owner

    Owner Guest

    thank you strcmp( &s1[ i - 3 ], s2 ); worked!!
     
    Owner, Feb 18, 2011
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.