is their any ay to compare strings rather than using the strcmp() fuction? If not, could somebody fully explain how it works? All I want to do is see if two strings are the same...
is their any ay to compare strings rather than using the strcmp() fuction? If not, could somebody fully explain how it works? All I want to do is see if two strings are the same...
C++ has overloaded the == operator to allow for the comparison of strings. Ex:
if(string1 == string2)
{
//ect
Why don't you want to use strcmp?
strcmp compares two strings and returns a value of 0 if they are the same. In the following example, the loop will run so long as strcmp is not returning 0.
Code:#include <stdio.h> #include <string.h> int main () { char Animal1[] = "dog"; char AnimalInput[20]; do { printf ("Guess what sort of pet I have? "); gets (AnimalInput); } while (strcmp (Animal1,AnimalInput) != 0); printf ("You guessed right, I have a %s\n",Animal1); return 0; }
Teach me for not realising I'm out of the "C" thread. Never mind, strcmp is still good to know
well you could make a function then,
use a pointer to the string, then you can compare it this way,
if(*ptr1 == *ptr2) // if the letter are the same,
{
// increment both pointers to check for the next letter
ptr1++;
ptr2++;
}
well you can do that 'while' the ptr is not pointing to nul,