comparing strings

This is a discussion on comparing strings within the C Programming forums, part of the General Programming Boards category; k this is whats my problem i can sound stupid but anyway Code: #include <stdio.h> int main(){ int a; scanf("%d",&a); ...

  1. #1
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109

    comparing strings

    k this is whats my problem i can sound stupid but anyway
    Code:
    #include <stdio.h>
    
    
             int main(){
    		           int a;
                     scanf("%d",&a);
                     if(a==1){printf("Blablaba");
                     if(a==2){printf("again bbalblablabla");}
    			     
             return 0;
             }
    so now if a=1 he says blabla
    now i wanna do the same thing with strings cause i wanna make some commands into my program like print save etc
    but it just wont work if i handle the string as an int
    i know this is an easy question but ive tried to find it out myself without any luck so ....

    LAmb

  2. #2
    zeldadude
    Guest
    Use the strcmp() function in string.h

    it works like this

    Code:
    if (strcmp(str1, str2) == 0){
        printf("They are the same string");
    }

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Code:
    if (strcmp(str1, str2) == 0){/*should be 1 coz i checked it and else it wont work*/
        printf("They are the same string");
    }
    thx anyway

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by GanglyLamb
    Code:
    if (strcmp(str1, str2) == 0){/*should be 1 coz i checked it and else it wont work*/
        printf("They are the same string");
    }
    thx anyway
    No. You're wrong. It should be zero.
    strcmp
    RETURN VALUE
    The strcmp() and strncmp() functions return an integer
    less than, equal to, or greater than zero if s1 (or the
    first n bytes thereof) is found, respectively, to be less
    than, to match, or be greater than s2.
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    strange if i say ==0 it wont work if i say ==1 it works perfectly well as long as it works im happy

  6. #6
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    broken euhm im using dev-c++ right now on this thing

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