string compare...

C

collinm

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
 
F

Fao, Sean

collinm said:
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;
}
 
D

David Resnick

collinm said:
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
 
B

Bill C

collinm said:
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.
 
K

Keith Thompson

Fao said:
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).
 
F

Fao, Sean

Keith said:
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.
 
F

Fao, Sean

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

Keith Thompson

Fao said:
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.
 
C

CBFalconer

Fao said:
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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top