comparing strings

This is a discussion on comparing strings within the C Programming forums, part of the General Programming Boards category; In this function, I am reading in a 'license plate' and comparing to all 'licence plates' in my array. If ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    5

    comparing strings

    In this function, I am reading in a 'license plate' and comparing to all 'licence plates' in my array. If there is a match, then null that part of the array (the car is leaving). It seemed to work fine but I discovered that if the plate number began with 'd' then the program nulls all plate numbers beginning with d. My for loop looks okay but I am not sure how to fix it. Suggestions???


    void Delcar (char cars [MAX][PLATE])//function to delete a car
    {

    int i,x;
    int counter;
    counter=CINIT;
    char tarray[PLATE]={'\0'};
    char ch;

    do
    {
    scanf("%s",tarray);

    {for (x=LINIT;x<MAX;x++)

    for (i=LINIT;i<=PLATE;i++)

    {
    if (cars[x][i]==tarray[i])
    {
    cars[x][i]='\0';
    printf("Car left space %d\n",x+1);

    counter++;
    }

    break;

    }
    }
    }
    while (counter<=CINIT);


    Options(); //function to call main menu
    scanf("%c",&ch);

    }

  2. #2
    Unregistered
    Guest
    You don't compare strings with ==

    Well, OK, so you can in C++, but not C.

    You must use either strcmp (string compare) or stricmp (insensitive string compare)


    instead of "if (cars[x][i]==tarray[i])"

    use "if(strcmp(cars[x], tarray)==0)" (if it equals 0, they're the same)

    or, to make it fancy do "if(!strcmp(car[x], tarray))"

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    Works fine now.

    Thanks!

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    I personally loathe un-natural return values, and even though strcmp is really a sort - friendly function, it is commonly used for "exact matching". A wrapper function does the trick:

    Code:
    
    int Identical( char s[], char t[] ) {
    
    if( strcmp( s, t ) == 0 )
    return 1;
    
    return 0;
    } 
    
    
    Of course the preferred may be:
    
    bool Identical( char *s, char *t ) {
    
    return strcmp( s, t ) == 0 ? true : false;
    }



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 09:44 PM
  3. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 AM
  4. comparing strings
    By infinitum in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 12:10 PM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 10:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21