How to check first x characters match a specific string

Discussion in 'C Programming' started by Angus, Nov 13, 2010.

  1. Angus

    Angus Guest

    I want to be able to check that a certain string, eg "abcde" is at the
    start of another string. how do I do that? strstr will find string
    but I need to check it is at beginning.
     
    Angus, Nov 13, 2010
    #1
    1. Advertising

  2. Angus

    Angus Guest

    On Nov 13, 11:29 pm, High Road to China Blue <>
    wrote:
    > In article <..com>,
    >
    >  Angus <> wrote:
    > > I want to be able to check that a certain string, eg "abcde" is at the
    > > start of another string.  how do I do that?  strstr will find string
    > > but I need to check it is at beginning.

    >
    > I use this to check if x begins with constant string y
    >
    >     #define begins(x,y) (strncmp(x,y,(sizeof y)-1)==0)
    >
    >     if (begins(x, "abcde")) ...
    >
    > --
    > Damn the living - It's a lovely life.           I'm whoever you want me to be.
    > Silver silverware - Where is the love?       At least I can stay in character.
    > Oval swimming pool - Where is the love?    Annoying Usenet one post at a time.
    > Damn the living - It's a lovely life.      I slipped the surly bonds of earth.


    Thanks. I also found strnicmp.
     
    Angus, Nov 13, 2010
    #2
    1. Advertising

  3. Angus

    Juan Guest

    On Nov 13, 5:10 pm, Angus <> wrote:
    > I want to be able to check that a certain string, eg "abcde" is at the
    > start of another string.  how do I do that?  strstr will find string
    > but I need to check it is at beginning.


    Hi
    strstr returns a pointer to the first occurrence of
    the pattern you are looking for, in another string

    So you can do something like

    pch = strstr(str,"abcde");
    if (pch && (pch == str)) {
    printf("pattern found at the begining");
    }
     
    Juan, Nov 13, 2010
    #3
  4. Angus

    robin Guest

    Angus wrote in message ...
    >I want to be able to check that a certain string, eg "abcde" is at the
    >start of another string. how do I do that? strstr will find string
    >but I need to check it is at beginning.


    In PL/I it's:

    If length(s) >= length(t) then
    if substr(s, 1, length(t)) = t then ...
     
    robin, Nov 13, 2010
    #4
  5. Angus <> wrote:

    > On Nov 13, 11:29 pm, High Road to China Blue
    > <> wrote:
    >> In article
    >> <

    > .com>,
    >>
    >>  Angus <> wrote:
    >> > I want to be able to check that a certain string, eg "abcde"
    >> > is at the start of another string.  how do I do that?  strstr
    >> > will find strin

    > g
    >> > but I need to check it is at beginning.

    >>
    >> I use this to check if x begins with constant string y
    >>
    >>     #define begins(x,y) (strncmp(x,y,(sizeof y)-1)==0)
    >>
    >>     if (begins(x, "abcde")) ...
    >>
    >> --

    [...]
    Oh, please don't quote signatures.

    > Thanks. I also found strnicmp.


    Well, I did not find strnicmp in IS-9899.

    You should be aware that the blue chinese's solution requires
    a pure string literal (and not a constant string) as second
    argument. It does not work for either
    const char *prefix = "prefix";
    with prefix as second argument or (directly)
    (const char *)"prefix"

    -- Ralf
     
    Ralf Damaschke, Nov 14, 2010
    #5
  6. Angus

    Andy K. Guest

    On Sat, 13 Nov 2010 15:10:38 -0800 (PST)
    Angus <> wrote:

    > I want to be able to check that a certain string, eg "abcde" is at the
    > start of another string. how do I do that? strstr will find string
    > but I need to check it is at beginning.


    Why not just use strncmp() with length of "abcde" as third argument?
     
    Andy K., Nov 14, 2010
    #6
  7. "Angus" <> wrote in message
    news:...
    >I want to be able to check that a certain string, eg "abcde" is at the
    > start of another string. how do I do that? strstr will find string
    > but I need to check it is at beginning.


    I am typing this crap directly in the news reader, so please forgive any
    typos!:
    ______________________________________________________
    int xstrfcmp(
    char const* src,
    char const* cmp
    ){
    for (; *src && *cmp; ++src, ++cmp)
    if (*src != *cmp) return 0;
    return (! *src && *cmp) ? 0 : 1;
    }
    ______________________________________________________


    That should do it...
     
    Chris M. Thomasson, Nov 14, 2010
    #7
  8. Angus

    Ike Naar Guest

    On 2010-11-14, Chris M. Thomasson <> wrote:
    > I am typing this crap directly in the news reader, so please forgive any
    > typos!:
    > ______________________________________________________
    > int xstrfcmp(
    > char const* src,
    > char const* cmp
    > ){
    > for (; *src && *cmp; ++src, ++cmp)
    > if (*src != *cmp) return 0;
    > return (! *src && *cmp) ? 0 : 1;
    > }


    Not exactly wrong, but unnecessarily complicated:

    for (; *cmp; ++src, ++cmp)
    if (*src != *cmp) return 0;
    return 1;

    would do.
     
    Ike Naar, Nov 14, 2010
    #8
  9. Angus

    Angus Guest

    I did it like this in the end:
    if(_tcsnicmp(src, tofind, _tcslen(tofind))==0)

    If you don't need unicode then
    if(strnicmp(src, tofind, strlen(tofind))==0)

    I needed a case insensitive match.

    Someone said they couldn't find strnicmp in the standard - which I
    will have to check. might make it less portable I suppose. It is
    available on windows.
     
    Angus, Nov 14, 2010
    #9
  10. Angus

    Geoff Guest

    On Sun, 14 Nov 2010 09:28:11 -0800 (PST), Angus
    <> wrote:

    >I did it like this in the end:
    >if(_tcsnicmp(src, tofind, _tcslen(tofind))==0)
    >
    >If you don't need unicode then
    >if(strnicmp(src, tofind, strlen(tofind))==0)
    >
    >I needed a case insensitive match.
    >
    >Someone said they couldn't find strnicmp in the standard - which I
    >will have to check. might make it less portable I suppose. It is
    >available on windows.


    strnicmp is POSIX. Microsoft has deprecated its use and substituted
    the non-standard _strnicmp function which they declare in <string.h>.
     
    Geoff, Nov 14, 2010
    #10
  11. Angus

    Alan Curry Guest

    In article <>,
    Geoff <> wrote:
    >
    >strnicmp is POSIX. Microsoft has deprecated its use and substituted
    >the non-standard _strnicmp function which they declare in <string.h>.


    I don't see any strnicmp in POSIX. strncasecmp is there.

    --
    Alan Curry
     
    Alan Curry, Nov 14, 2010
    #11
  12. Juan wrote:
    > On Nov 13, 5:10 pm, Angus <> wrote:
    >> I want to be able to check that a certain string, eg "abcde" is at the
    >> start of another string. how do I do that? strstr will find string
    >> but I need to check it is at beginning.

    >
    > Hi
    > strstr returns a pointer to the first occurrence of
    > the pattern you are looking for, in another string
    >
    > So you can do something like
    >
    > pch = strstr(str,"abcde");
    > if (pch && (pch == str)) {
    > printf("pattern found at the begining");
    > }


    Or to put if more simply

    if (strstr(str,"abcde") == str) {
    printf("pattern found at the beginning");
    }
     
    J. J. Farrell, Nov 15, 2010
    #12
  13. Angus

    Ike Naar Guest

    On 2010-11-15, J. J. Farrell <> wrote:
    > if (strstr(str,"abcde") == str) {
    > printf("pattern found at the beginning");
    > }


    This method is wasteful if str is long and does not start with the
    pattern; in that case, strstr() may have to process a lot of
    uninteresting data.
     
    Ike Naar, Nov 15, 2010
    #13
  14. Angus

    Chad Guest

    On Nov 15, 1:24 am, Ike Naar <> wrote:
    > On 2010-11-15, J. J. Farrell <> wrote:
    >
    > > if (strstr(str,"abcde") == str) {
    > >     printf("pattern found at the beginning");
    > > }

    >
    > This method is wasteful if str is long and does not start with the
    > pattern; in that case, strstr() may have to process a lot of
    > uninteresting data.


    How could strstr() possibly process a lot of uninteresting data?

    Chad
     
    Chad, Nov 15, 2010
    #14
  15. Angus

    James Waldby Guest

    On Mon, 15 Nov 2010 08:08:21 -0800, Chad wrote:
    > On Nov 15, 1:24 am, Ike Naar ...freeshell.org> wrote:
    >> On 2010-11-15, J. J. Farrell ...bcs.org.uk> wrote:
    >>
    >> > if (strstr(str,"abcde") == str) {
    >> >     printf("pattern found at the beginning");
    >> > }

    >> This method is wasteful if str is long and does not start with the
    >> pattern; in that case, strstr() may have to process a lot of
    >> uninteresting data.

    >
    > How could strstr() possibly process a lot of uninteresting data?


    Presumably Naar meant irrelevant rather than boring.

    Suppose str is a few petabytes of letters, nul terminated, with
    no "abcde" substring in it; strstr would scan through str for a
    day or two all to no avail, when use of strncmp or strncasecmp
    would answer the real question in a handful of nanoseconds.

    --
    jiw
     
    James Waldby, Nov 15, 2010
    #15
  16. Ike Naar wrote:
    > On 2010-11-15, J. J. Farrell <> wrote:
    >> if (strstr(str,"abcde") == str) {
    >> printf("pattern found at the beginning");
    >> }

    >
    > This method is wasteful if str is long and does not start with the
    > pattern; in that case, strstr() may have to process a lot of
    > uninteresting data.


    Certainly, it was meant as a simplification of Juan's suggestion, not as
    a recommended solution. It might just make sense if you know str is
    short and don't already know the length of the string you're looking
    for, but use of strncmp() would almost always be more efficient (perhaps
    always).
     
    J. J. Farrell, Nov 17, 2010
    #16
  17. Angus

    Tim Rentsch Guest

    Ike Naar <> writes:

    > On 2010-11-14, Chris M. Thomasson <> wrote:
    >> I am typing this crap directly in the news reader, so please forgive any
    >> typos!:
    >> ______________________________________________________
    >> int xstrfcmp(
    >> char const* src,
    >> char const* cmp
    >> ){
    >> for (; *src && *cmp; ++src, ++cmp)
    >> if (*src != *cmp) return 0;
    >> return (! *src && *cmp) ? 0 : 1;
    >> }

    >
    > Not exactly wrong, but unnecessarily complicated:
    >
    > for (; *cmp; ++src, ++cmp)
    > if (*src != *cmp) return 0;
    > return 1;
    >
    > would do.


    That's still more complicated than necessary - just this

    return *cmp == 0 || *src == *cmp && xstrfcmp( src+1, cmp+1 );

    suffices.
     
    Tim Rentsch, Nov 18, 2010
    #17
    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. Replies:
    5
    Views:
    943
    X-Centric
    Jun 30, 2005
  2. Old Echo
    Replies:
    1
    Views:
    182
    Adam Shelly
    Sep 4, 2008
  3. Replies:
    2
    Views:
    86
  4. Jack
    Replies:
    36
    Views:
    302
    Jürgen Exner
    May 31, 2006
  5. hofer
    Replies:
    5
    Views:
    115
    Ben Morrow
    Sep 12, 2008
Loading...

Share This Page