Thread: String prob

  1. #1
    Unregistered
    Guest

    Angry String prob

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

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    C++ has overloaded the == operator to allow for the comparison of strings. Ex:

    if(string1 == string2)
    {
    //ect

  3. #3
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    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;
    }

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Teach me for not realising I'm out of the "C" thread. Never mind, strcmp is still good to know

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    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,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM