Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > string compare...

Reply
Thread Tools

string compare...

 
 
collinm
Guest
Posts: n/a
 
      03-24-2005
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

 
Reply With Quote
 
 
 
 
Fao, Sean
Guest
Posts: n/a
 
      03-24-2005
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
 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      03-24-2005
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
 
Reply With Quote
 
Bill C
Guest
Posts: n/a
 
      03-24-2005

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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-24-2005
"Fao, Sean" <(E-Mail Removed)-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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Fao, Sean
Guest
Posts: n/a
 
      03-24-2005
Keith Thompson wrote:
> "Fao, Sean" <(E-Mail Removed)-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
 
Reply With Quote
 
Fao, Sean
Guest
Posts: n/a
 
      03-24-2005
Fao, Sean wrote:
> Keith Thompson wrote:
>
>> "Fao, Sean" <(E-Mail Removed)-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
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      03-24-2005
On 24 Mar 2005 07:50:34 -0800, in comp.lang.c , "collinm"
<(E-Mail Removed)> 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>
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-24-2005
"Fao, Sean" <(E-Mail Removed)-WANT-NO-SPAM> writes:
> Fao, Sean wrote:
>> Keith Thompson wrote:
>>
>>> "Fao, Sean" <(E-Mail Removed)-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) (E-Mail Removed) <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.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-25-2005
"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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments