473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Nov 14 '05 #1
9 2079
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
Nov 14 '05 #2
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
Nov 14 '05 #3

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.

Nov 14 '05 #4
"Fao, Sean" <en**********@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) ks***@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.
Nov 14 '05 #5
Keith Thompson wrote:
"Fao, Sean" <en**********@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
Nov 14 '05 #6
Fao, Sean wrote:
Keith Thompson wrote:
"Fao, Sean" <en**********@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
Nov 14 '05 #7
On 24 Mar 2005 07:50:34 -0800, in comp.lang.c , "collinm"
<co*****@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>
Nov 14 '05 #8
"Fao, Sean" <en**********@yahoo.comI-WANT-NO-SPAM> writes:
Fao, Sean wrote:
Keith Thompson wrote:
"Fao, Sean" <en**********@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) ks***@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.
Nov 14 '05 #9
"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
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two...
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
6
by: Maileen | last post by:
Hi, I have the following code : Function GetRequestType(ByVal EvDt As String, ByVal StPeriod As String, ByVal EdPeriod As String, ByVal TaskType As String) As Integer Dim strtest As String Dim...
10
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
11
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one...
11
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
1
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.