0

Hi all:

Say, my program is about to receive a string "ABC", and I want to test if the string really contains "ABC". What is the best way to test it?
I am thinking of read all the characters from the incoming string one by one, but not sure which function is to use.
P.S. string source is not standard input.
Thanks

6
Contributors
13
Replies
14
Views
11 Years
Discussion Span
Last Post by SpS
0

Is this in C or C++? What is your definition of 'string'?

If you're using an array of characters with a zero at the end, then you can use the procedure strcmp(x, y) to compare the contents of two arrays of characters, whose memory addresses are x and y. For example:

char z[20] = "Foobar";
char t[12] = "Foobar";
int ret = strcmp(t, z);

Both t and z are regarded as equal strings by strcmp. strcmp returns 0 when two strings are equal, a negative value when the string on the left is 'less' than the string on the right, and a positive value when the string on the right is 'less' than the string on the left (maybe it's the other way around -- I forget and you'd have to try and see).

Now, if you're using C++, you'll want to store your information in std::string objects, since that's just easier. You can compare std::string objects using the normal == operator. If you decide to go with char*, C-style strings, in C++, you can still use strcmp, though.

Note that in C you should include #include <string.h> at the top of your file that uses strcmp, since the procedure is declared in that header file.

0

>char z[20] = "Foobar";
char t[12] = "Foobar";

Can be replaced with:-

char z[] = "Foobar";
char t[] = "Foobar";

0

Yes. Thank you all for your generous help.

There is another problem here.

codes:
char *buff;
char buffer_read[100]="some_string";
PING[]="PING";

buff = buffer_read;

if(strcmp(buff, PING)==0)
{
do something;
}
else
{
bugger!
}

The "if" statement always returns a non-zero value, with some help from the debugger, I found out that "buff" actually only represents only the very first character of the string, it is not what I want.
Is there any chance to keep all the variable declarations and initializaitons but somehow get the pointer "buff" represents the whole string?

Thanks

0

Hi guys:

I found the solution: memcmp().
The key is the third parameter of this memcmp() funciton.

Thanks

0

Or

char *z = "Foobar";
char *t = "Foobar";

Note:
http://c-faq.com/decl/strlitinit.html
[edit]Just mentioning this because it's a pet peeve of mine.[/edit]

I found the solution: memcmp().
The key is the third parameter of this memcmp() funciton.

To compare C-style strings, use strcmp as previously mentioned.

#include <stdio.h>
#include <string.h>

int main ()
{
   char buffer_read[100] = "some_string";
   char PING[] = "PING";
   if ( strcmp(buffer_read, PING) == 0 )
   {
      printf("\"%s\" == \"%s\"\n", buffer_read, PING);
   }
   else
   {
      printf("\"%s\" != \"%s\"\n", buffer_read, PING);
   }   
   if ( strcmp(buffer_read, "some_string") == 0 )
   {
      printf("\"%s\" == \"%s\"\n", buffer_read, "some_string");
   }
   else
   {
      printf("\"%s\" != \"%s\"\n", buffer_read, "some_string");
   }   
   return 0;
}

/* my output
"some_string" != "PING"
"some_string" == "some_string"
*/

Icky example.

0

i think you should develop a sequence tracking algortihm. my example algorith...

char string[100] = "string of ABC and so on.. and ABC so forth.."
char tracker[3] = "ABC"
char buffer[40]="";

for(i=0; i< strlen(tracker)-1 ; i++){
buffer = string;
}

for(x=0; x< strlen(string);x++){
buffer[x]=string[x];
if(strcmp(buffer,tracker)==0){
return true;
/* it contains a word which is the same as in the tracker*/
}
buffer[x-1] = buffer[x];
}

any clarification email me <<email snipped>>

0

How is that link related to my reply???

I don't know if you saw my edit. Basically I think the pointer version is just a crappy tidbit to mention because it leads to other problems.

i think you should develop a sequence tracking algortihm. my example algorith...

I think you could work on this a bit first. And avoid using strlen(string) as a loop condition. And keep it on the site.

0

Yaa..I didn't see your edit...but since OP was simply comparing so I guess pointers version is no problem.

But, yes we should be careful about this.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.