Thread: compare strings

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    compare strings

    i have two arrays one is a text file "month1" and the other is user defined "month". my purpose is to compare the month entered by the user to the month in the array if they are the same allow the rest of code to execute if not ask the user to enter another month.

    Code:
    printf( "Enter Month: " );
    scanf( "%s", Month );
    if (strncmp(Month, month1, 7) ==0)
    {prinf("not the same");

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    strncmp() - no.

    strcmp() - yes.

    December needs 9 spaces: 8 char's and one null char: '\0' on the end.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    There's an error at line 3.
    man strncmp
    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.
    In other words, if both strings are equal, strcmp and strncmp should return 0.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    For some reason I can't get it figured out when the user enters the month it should compare it to month1 if they match proceed if stop but the program continues no matter what is entered

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    *if not

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Try something like this:

    Code:
    if(/*Insert here a condition that checks if months do not match*/)
    {
      printf("Months do not match.\n");
      return 1;
    }
    
    /* Insert here code for if the months match...*/
    Returning from the main function is a good way to end the program.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jamal View Post
    i have two arrays one is a text file "month1" and the other is user defined "month". my purpose is to compare the month entered by the user to the month in the array if they are the same allow the rest of code to execute if not ask the user to enter another month.

    Code:
    printf( "Enter Month: " );
    scanf( "%s", Month );
    if (strncmp(Month, month1, 7) ==0)
    {prinf("not the same");
    actually when strcmp() or strncmp() returns 0 they *are* the same...

    Code:
    printf( "Enter Month: " );
    scanf( "%s", Month );
    if (strncmp(Month, month1, 7) !=0)
      {
        printf("not the same"); 
      }
    Also keep in mind that this is CaSe SeNsItIvE ... January is not the same as january.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I compare strings?
    By errigour in forum C Programming
    Replies: 9
    Last Post: 11-16-2010, 05:54 PM
  2. Compare Two Strings?
    By Paul22000 in forum C++ Programming
    Replies: 9
    Last Post: 05-24-2008, 05:09 PM
  3. Trying to compare to strings...
    By imortal in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2003, 12:27 PM
  4. compare contents of strings
    By Max in forum C Programming
    Replies: 2
    Last Post: 10-26-2002, 06:28 PM
  5. how do i compare strings
    By bart in forum C++ Programming
    Replies: 17
    Last Post: 08-30-2001, 09:17 PM