Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   string compare... (http://www.velocityreviews.com/forums/t437468-string-compare.html)

collinm 03-24-2005 03:50 PM

string compare...
 
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


Fao, Sean 03-24-2005 04:00 PM

Re: string compare...
 
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

David Resnick 03-24-2005 04:13 PM

Re: string compare...
 
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

Bill C 03-24-2005 04:20 PM

Re: string compare...
 

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.


Keith Thompson 03-24-2005 08:01 PM

Re: string compare...
 
"Fao, Sean" <enceladus311@yahoo.comI-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) kst-u@mib.org <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.

Fao, Sean 03-24-2005 08:30 PM

Re: string compare...
 
Keith Thompson wrote:
> "Fao, Sean" <enceladus311@yahoo.comI-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 03-24-2005 08:35 PM

Re: string compare...
 
Fao, Sean wrote:
> Keith Thompson wrote:
>
>> "Fao, Sean" <enceladus311@yahoo.comI-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

Mark McIntyre 03-24-2005 09:48 PM

Re: string compare...
 
On 24 Mar 2005 07:50:34 -0800, in comp.lang.c , "collinm"
<collinm@laboiteaprog.com> 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>

Keith Thompson 03-24-2005 11:26 PM

Re: string compare...
 
"Fao, Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> writes:
> Fao, Sean wrote:
>> Keith Thompson wrote:
>>
>>> "Fao, Sean" <enceladus311@yahoo.comI-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) kst-u@mib.org <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.

CBFalconer 03-25-2005 05:25 AM

Re: string compare...
 
"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




All times are GMT. The time now is 01:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.