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

Dani AI

Generated

Quick clarification first: do you want exact equality (the whole incoming string equals "ABC") or a substring test ("ABC" appears anywhere inside)? The replies from , and already show both directions (standard helpers and a manual scan). For most C code with NUL-terminated text use the C library substring/equality helpers; in C++ prefer std::string and its member functions.

A minimal, safe pattern — substring check in C and the equivalent in C++:

/* C: returns non-zero if `needle` appears in `haystack` */
#include <string.h>
int contains(const char *haystack, const char *needle) {
    return haystack && needle && *needle && strstr(haystack, needle) != NULL;
}

/* C++: prefer std::string */
#include <string>
bool contains_cpp(const std::string &s, const std::string &needle) {
    return s.find(needle) != std::string::npos;
}

Diagnosing the "buff only shows first character" symptom (as in 's later posts): common causes are not pointer syntax but data problems or lifetime issues. Checklist and quick diagnostics:

  • Confirm the declaration is char *buff; and you assign it to a valid, live buffer. A pointer to a stack buffer that goes out of scope becomes invalid.
  • Check NUL-termination. If the buffer lacks a terminating '\0', strcmp/strstr will misbehave; memcmp can appear to "work" because it compares a fixed byte count.
  • Look for trailing CR/LF, spaces or non-printables (often from network input). Print a hex dump to be sure:
for (size_t i = 0; i < strlen(buff); ++i)
    printf("%02x ", (unsigned char)buff[i]);
  • If input contains CR/LF, trim them before comparing.

Final tips: prefer length-limited APIs when you don't trust termination (e.g., strncmp or explicit length checks), avoid memcmp for C-strings unless you manage lengths precisely, and in C++ use std::string to avoid manual lifetime/termination bugs. If you need high-performance substring search on large data, consider KN P/Boyer‑Moore algorithms — but for typical uses strstr/std::string::find are simpler and sufficient.

Recommended Answers

All 13 Replies

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.

Thanks man

Member Avatar for Member #46692

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

Can be replaced with:-

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

Or

char *z = "Foobar";
char *t = "Foobar";
Member Avatar for Member #46692

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

What's with the magic numbers, 20 and 12 anyway?
:sad:

My guess--random numbers

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

Hi guys:

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

Thanks

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.

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.