-1

Possible Duplicate:
How to compare pointer to strings in C

How to compare the characters in a buffer with a string?

3
  • Can you explain your problem clearly? Commented Feb 22, 2011 at 6:03
  • 1
    If possible, give an example. Commented Feb 22, 2011 at 6:04
  • i tried comparing the contents of a buffer with a string. like strcmp(buffer,"change") Commented Feb 22, 2011 at 6:19

4 Answers 4

2

By buffer, I assume its not NULL terminated. Then you can not use strcmp, instead you can use strncmp.

Sign up to request clarification or add additional context in comments.

2 Comments

it is specifying as too few arguments: strncmp(buffer,"change")
@neha: It takes 3 arguments (see link). Third argument is the number of characters in the buffer. Note that it matches substring. If you want the exact match then you need to compare the string length with number of characters in the buffer.
1

Assuming, the buffer is an array of characters. You can compare character by character. Example -

char buffer[] = { 'a','b','c' };
char* str = "b";

int i=0;
while( i<3 )
{
    if( buffer[i] == *str )
        printf("\n Equal \n" );
    else
        printf("\n Not Equal \n" );

    ++i;
}

The above code should give you basic idea of how to implement. Results : IdeOne

Things you need to think of to answer the question -

  • What if the value pointed by str is "abc"?
  • What if the entire buffer needs to be compared to value pointed by char* ( i.e., buffer is equal to value pointed by char* ?

Comments

1

Something that throws a lot of people off at first is that strcmp returns 0 if the strings match, so you usually use something like if (!strcmp(buffer, "change"))

Comments

0
int strcmp ( const char *s1, const char *s2 );

Try this. It will help you.

1 Comment

i tried strcmp(buffer, "change").

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.