Like Tree1Likes
  • 1 Post By nonoob

String Compare

This is a discussion on String Compare within the C Programming forums, part of the General Programming Boards category; Hi Everyone! How do I compare the array of strings with a string? I tried with strcmp.I want to check ...

  1. #1
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59

    String Compare

    Hi Everyone!
    How do I compare the array of strings with a string? I tried with strcmp.I want to check which month has the user entered,accordingly take the number corresponding for the month for calculations.Getting confused when it comes to strings,please help.


    Code:
    void get_day(int y,char* m,int d,int lp)
    {
        int c,a,i,cnt=0,b,e,sum,res,z;
        int cent[10]={4,2,0,6,4,2,0,6,4,2};
        int month_code[12]={0,3,3,6,1,4,6,2,5,0,3,5};
        char month[12][5]={"jan","feb","mar","apr","may","jun","july","aug","sept","oct","nov","dec"};
        char day[7][10]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
        z=y/100;
        b=y%100;    //year
        c=b/4;
        for(i=17;i<=26;i++)
        {
            if(i==z)
            a=cent[cnt];    //century
            else
            cnt++;
        }
        printf("a=%d\n",a);
        printf("b=%d\n",b);
        printf("c=%d\n",c);
        printf("d=%d\n",d);
        /*if(lp==TRUE)
        {
            if(*m=='jan')
                e=6;
    
    
            else if(*m=='feb')
         }       e=2;
        else
        {*/
            for(i=0;i<12;i++)
            {
                if(strcmp(month[i],m))      //problem part
                {
                        printf("val of i=%d\n",i);    //test
                        e=month_code[i];
                        break;  
                }
    
    
            }
        //}
        printf("e=%d\n",e);
        sum=a+b+c+d+e;
        res=sum%7;
        printf("sum=%d\n",sum);
        printf("res=%d\n",res);
        printf("The day is %s\n",day[res]);
    }
    int leap_year(int year)
    {
        if(year%400==FALSE && year%100!=FALSE || year%4==FALSE)
        return TRUE;
        else
        return FALSE;
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I think you are checking if the strings are equal... so it should be
    if (0 == strcmp(month[i], m))
    shruthi likes this.

  3. #3
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Thank you,somehow I overlooked that and thought the problem was with strings.It's working fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. Compare 2 String
    By nitediver in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 07:48 AM
  3. String Compare
    By almo89 in forum C Programming
    Replies: 1
    Last Post: 11-04-2005, 07:13 PM
  4. need help in string compare
    By afzan in forum C++ Programming
    Replies: 13
    Last Post: 03-31-2005, 06:05 AM
  5. need help on string compare
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 06-07-2002, 08:55 PM

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