Thread: strcmp

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    Unhappy Don't quite understand..

    if user inputs Monday and 'n' = 3, your output should be Thursday.

    user input = day of the wk & number of days from now (between 1000<_ n <_10000)

    they want to use a strcmp and the mod operator

    Im a little lost on why...and how
    Last edited by digy; 05-13-2003 at 11:07 AM.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    strcmp is probably used to determine which day was entered. The modulus operator is used to wrap around an array so that if you go past the end, you start over at the beginning:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define NDAYS 7
    
    static char *days[] = {
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday",
    };
    
    static int dayValue(char *day)
    {
        int i;
    
        for (i = 0; i < NDAYS; i++)
        {
            if (strcmp(day, days[i]) == 0)
                break;
        }
    
        return i;
    }
    
    int main(void)
    {
        int n = 3;
    
        printf("%s\n", days[(dayValue("Monday") + n) % NDAYS]);
        printf("%s\n", days[(dayValue("Sunday") + n) % NDAYS]);
        printf("%s\n", days[(dayValue("Thursday") + n) % NDAYS]);
    
        return 0;
    }
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    thank you

    thank you for your quick response.
    Not working for me but it's coming along.

    QUESTION:
    The user inputs the day (monday) then inputs a number between 1000 and 10000 days.
    How would you figure out what day it is from the number they inputted?

  4. #4
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >The user inputs the day (monday) then inputs a number between 1000 and 10000 days.
    The above method should work for any number of days provided you don't take leap days into account.
    p.s. What the alphabet would look like without q and r.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Not working for me but it's coming along.
    >The user inputs the day (monday) then inputs a number between 1000 and 10000 days.

    strcmp is case sensitive ("monday" is not the same as "Monday").

    This is a slightly modified version of Brighteyes' code to demonstrate the case sensitivity.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define NDAYS 7
    
    static const char *days[] = {
       "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday",
    };
    
    static int dayValue(const char *day)
    {
       int i;
       for ( i = 0; i < NDAYS; i++ )
       {
          if ( strcmp(day, days[i]) == 0 )
          {
             return i;
          }
       }
       return -1;
    }
    
    void foo(const char *day, int n)
    {
       int dow = dayValue(day);
       printf("%s + %d = ", day, n);
       if( dow >= 0)
       {
          puts(days[(dow + n) % NDAYS]);
       }
       else
       {
          puts("not found");
       }
    }
    
    int main(void)
    {
       const char myday[] = "Friday";
       int n = 1234;
       foo("Monday", 5);
       foo("monday", 5);
       foo("Tuesday", 9);
       foo(myday, n);
       return 0;
    }
    
    /* my output
    Monday + 5 = Saturday
    monday + 5 = not found
    Tuesday + 9 = Thursday
    Friday + 1234 = Sunday
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    thanks

    thank you guys.
    but don't have to use functions this time

  7. #7
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    Am I the only one in doing strings that always puts them as caps? I always toupper everything thats inputted by the user. Maybe its bad programming on my end. :P
    To error is human, to really foul things up requires a computer

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    No, Twiggy, it's not bad programming. I always use toupper() also. It's just whatever habit you get into.

    Walt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM