string compare...

Discussion in 'C Programming' started by collinm, Mar 24, 2005.

  1. collinm

    collinm Guest

    hi

    is there a function who allow to compare string and start at position X

    example

    str="print.jpp"
    ext="jpg"

    i would like to start to compare str at position 6

    any idea?

    thanks
     
    collinm, Mar 24, 2005
    #1
    1. Advertising

  2. collinm

    Fao, Sean Guest

    collinm wrote:
    > hi
    >
    > is there a function who allow to compare string and start at position X
    >
    > example
    >
    > str="print.jpp"
    > ext="jpg"
    >
    > i would like to start to compare str at position 6


    Not directly; but, you can do something like this:

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char *name = "picture.jpg";

    while (*name++ != '.')
    ;

    if (!strncmp(name, "jpg", 3))
    printf("%s\n", name);

    return 0;
    }

    --
    Sean
     
    Fao, Sean, Mar 24, 2005
    #2
    1. Advertising

  3. collinm wrote:

    > hi
    >
    > is there a function who allow to compare string and start at position X
    >
    > example
    >
    > str="print.jpp"
    > ext="jpg"
    >
    > i would like to start to compare str at position 6
    >
    > any idea?
    >
    > thanks
    >


    strcmp(str+6, ext)

    Of course, you should make sure that str+6 isn't past the end of
    the region pointed to by str.

    Good way to do this sort of thing:
    const char *dot = strrchr(str, '.');
    if (dot != NULL) {
    if (strcmp(dot+1, ext) == 0) {
    ....
    }
    }

    -David
     
    David Resnick, Mar 24, 2005
    #3
  4. collinm

    Bill C Guest

    collinm wrote:
    > hi
    >
    > is there a function who allow to compare string and start at position

    X
    >
    > example
    >
    > str="print.jpp"
    > ext="jpg"
    >
    > i would like to start to compare str at position 6
    >
    > any idea?
    >
    > thanks


    If ther is always a know delimiting character for starting the compare,
    (that is, you always want to ignore the filename and just look at the
    extension after the 'dot' char), then you may use the strtok function
    to create a new string parsed at the token.

    Cheers,
    Bill C.
     
    Bill C, Mar 24, 2005
    #4
  5. "Fao, Sean" <-WANT-NO-SPAM> writes:
    > collinm wrote:
    >> hi
    >> is there a function who allow to compare string and start at
    >> position X
    >> example
    >> str="print.jpp"
    >> ext="jpg"
    >> i would like to start to compare str at position 6

    >
    > Not directly; but, you can do something like this:
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(void)
    > {
    > char *name = "picture.jpg";
    >
    > while (*name++ != '.')
    > ;
    >
    > if (!strncmp(name, "jpg", 3))
    > printf("%s\n", name);
    >
    > return 0;
    > }


    Where the phrase "something like this" covers handling the case where
    the string doesn't contain a '.' character.

    You'll probably also want to think about what to do if the string
    contains two or more '.' characters. Hint: strrchr() (which returns a
    null pointer if the character isn't found).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 24, 2005
    #5
  6. collinm

    Fao, Sean Guest

    Keith Thompson wrote:
    > "Fao, Sean" <-WANT-NO-SPAM> writes:
    >
    >>collinm wrote:
    >>
    >>>hi
    >>>is there a function who allow to compare string and start at
    >>>position X
    >>>example
    >>>str="print.jpp"
    >>>ext="jpg"
    >>>i would like to start to compare str at position 6

    >>
    >>Not directly; but, you can do something like this:
    >>
    >>#include <stdio.h>
    >>#include <string.h>
    >>
    >>int main(void)
    >>{
    >> char *name = "picture.jpg";
    >>
    >> while (*name++ != '.')
    >> ;
    >>
    >> if (!strncmp(name, "jpg", 3))
    >> printf("%s\n", name);
    >>
    >> return 0;
    >>}

    >
    >
    > Where the phrase "something like this" covers handling the case where
    > the string doesn't contain a '.' character.
    >
    > You'll probably also want to think about what to do if the string
    > contains two or more '.' characters. Hint: strrchr() (which returns a
    > null pointer if the character isn't found).


    I kind of had a hidden agenda in my post. I had a feeling that the OP
    didn't understand pointers as well as he should and I demonstrated (sort
    of ;-) ) how the other functions work. Whether or not it worked remains
    a mystery.

    But thanks, nonetheless, for pointing out a couple of potential design
    flaws that I should have mentioned.

    --
    Sean
     
    Fao, Sean, Mar 24, 2005
    #6
  7. collinm

    Fao, Sean Guest

    Fao, Sean wrote:
    > Keith Thompson wrote:
    >
    >> "Fao, Sean" <-WANT-NO-SPAM> writes:
    >>
    >>> while (*name++ != '.')
    >>> ;

    >
    > But thanks, nonetheless, for pointing out a couple of potential design
    > flaws that I should have mentioned.
    >


    By the way, this while loop also assumes that "name" is a properly
    fomatted C string.

    --
    Sean
     
    Fao, Sean, Mar 24, 2005
    #7
  8. On 24 Mar 2005 07:50:34 -0800, in comp.lang.c , "collinm"
    <> wrote:

    >hi
    >
    >is there a function who allow to compare string and start at position X
    >
    >str="print.jpp"
    >ext="jpg"


    >i would like to start to compare str at position 6


    how about
    strcnmp(ext, str+6, strlen(ext));


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Mar 24, 2005
    #8
  9. "Fao, Sean" <-WANT-NO-SPAM> writes:
    > Fao, Sean wrote:
    >> Keith Thompson wrote:
    >>
    >>> "Fao, Sean" <-WANT-NO-SPAM> writes:
    > >>
    >>>> while (*name++ != '.')
    >>>> ;

    > >
    >> But thanks, nonetheless, for pointing out a couple of potential design
    >> flaws that I should have mentioned.
    >>

    >
    > By the way, this while loop also assumes that "name" is a properly
    > fomatted C string.


    Not necessarily. Its behavior is well-defined if name points to an
    array of characters that includes a '.' character, even if there's no
    trailing '\0'. (The following strncmp() and printf() calls make some
    more assumptions; I'm too lazy to work out the details, but note that
    the arguments to strncmp() needn't point to strings.)

    Some cases the OP should consider are:

    "" (empty string)
    "foobar" (no '.')
    "foo.bar.jpg" (multiple '.'s)
    "foo.JPG" (matches, but case-insensitively)
    "foobar.jpgggg" (matches ".jpg", but with extra characters)

    Other edge cases are a null pointer and a pointer to a character array
    not terminated by a '\0'; it might suffice to treat these as undefined
    behavior (errors that should have been caught by the caller).

    Another edge case is an invalid non-null pointer, such as one that
    points to an object that no longer exists. This isn't worth worrying
    about, since there's no portable way to detect it, and the program
    will have invoked undefined behavior just by evaluating it, before the
    function is even called.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 24, 2005
    #9
  10. collinm

    CBFalconer Guest

    "Fao, Sean" wrote:
    > collinm wrote:
    >>
    >> is there a function who allow to compare string and start at
    >> position X
    >>
    >> example
    >>
    >> str="print.jpp"
    >> ext="jpg"
    >>
    >> i would like to start to compare str at position 6

    >
    > Not directly; but, you can do something like this:


    Yes, directly. Assuming position 6 is known to lie within the
    string str, the OP simply wants to write:

    strcmp(ext, str+6)

    However, if the objective is to check what follows the final '.' in
    str, the answer is much different. I would write a separate
    routine, such as:

    /* Return zero for string following final '.' matching *ext */
    /* else non-zero */
    int cmpext(const char *fn, const char *ext)
    {
    char *p;

    p = NULL;
    while (*fn) {
    if ('.' == *fn) p = fn;
    fn++;
    }
    if (!p) return 1; /* or -1 if you prefer */
    return strcmp(++p, ext);
    } /* cmpext untested */

    and it will probably be worthwhile to expand strcmp in line.

    As is often the case, the OP asks for a function rather than
    describing what he wants to do.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Mar 25, 2005
    #10
    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. jrthor2

    help with string compare

    jrthor2, Apr 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    335
    jrthor2
    Apr 16, 2004
  2. =?Utf-8?B?YzY3NjIyOA==?=

    how compare object with string

    =?Utf-8?B?YzY3NjIyOA==?=, Feb 14, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    429
    Steven Cheng[MSFT]
    Feb 15, 2006
  3. Replies:
    10
    Views:
    796
  4. Replies:
    10
    Views:
    786
    Roland Pibinger
    Jan 24, 2007
  5. Replies:
    2
    Views:
    460
Loading...

Share This Page