How to check first x characters match a specific string

A

Angus

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

Angus

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

Juan

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");
}
 
R

robin

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

Ralf Damaschke

[...]
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
 
A

Andy K.

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?
 
C

Chris M. Thomasson

Angus said:
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...
 
I

Ike Naar

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

Angus

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

Geoff

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

Alan Curry

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

J. J. Farrell

Juan said:
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");
}
 
I

Ike Naar

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

Chad

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
 
J

James Waldby

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

J. J. Farrell

Ike said:
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).
 
T

Tim Rentsch

Ike Naar said:
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.
 

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

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top