how to modify strcmp function

This is a discussion on how to modify strcmp function within the C Programming forums, part of the General Programming Boards category; hi everybody I hope you are fine I am trying to change a little bit in the format of the ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    how to modify strcmp function

    hi
    everybody I hope you are fine
    I am trying to change a little bit in the format of the strcmp function ;;;
    what I need from the program is to compare two lower case and upper case strings and tell that they are the same
    I have a try on that but the problem is if I type two different strings it also give me that they are equivalent
    this is my code and thanks for your time
    Code:
    #include <string.h>
    #include <stdio.h>
    int strcmp1 (char s1[], char s2[]);
    
    
    
    main() {
         char s1[81], s2[81];
         gets(s1);
         gets(s2);
         
         if (strcmp1(s1, s2))
            printf("The strings are equal\n");
         else 
        printf("sorry");
        return 0;
    }
    
    
    int strcmp1 (char s1[], char s2[]) {
     int i;
    for (i=0; s1[i]!='\0'||s2[i]!='\0'; i++ ) {
      if(s1[i]!=s2[i])
        return s1[i]-s2[i];
             }
    return s1[i]-s2[i];
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by asteroid1122 View Post
    hi
    everybody I hope you are fine
    I am trying to change a little bit in the format of the strcmp function ;;;
    what I need from the program is to compare two lower case and upper case strings and tell that they are the same
    I have a try on that but the problem is if I type two different strings it also give me that they are equivalent
    this is my code and thanks for your time
    Code:
    #include <string.h>
    #include <stdio.h>
    int strcmp1 (char s1[], char s2[]);
    
    
    
    main() {
         char s1[81], s2[81];
         gets(s1);
         gets(s2);
         
         if (strcmp1(s1, s2))
            printf("The strings are equal\n");
         else 
        printf("sorry");
        return 0;
    }
    
    
    int strcmp1 (char s1[], char s2[]) {
     int i;
    for (i=0; s1[i]!='\0'||s2[i]!='\0'; i++ ) {
      if(s1[i]!=s2[i])
        return s1[i]-s2[i];
             }
    return s1[i]-s2[i];
    }
    Since you're returning the ASCII difference of the chars, which will never be 0 when the strings aren't equal. The main catch is that if you input two equal strings, then it'll show "sorry" coz, in that case the ASCII value will be 0 which is returned and the else part gets printed. Also never use gets, use fgets instead.
    PS: I would have converted one of the strings to either lower or uppercase and then used the standard strcmp function.
    Last edited by BEN10; 08-22-2009 at 07:24 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Also use

    int main(void)

    as you are returning 0 from the main()

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Also a few suggestions on how to do the comparison of two strings

    1. Ask input from the user
    2. Pass the pointer to the char array to the string comparison functions (but before that use tolower / toupper so that the case is same for both the strings)
    3. Strings are equal if and only if they have the same set of chars till the null terminated char is reached else exit out saying they are unequal.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    unfortunately that does not work or I did not do it well
    please help me with the code thanx

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Do you mean that you're looking for the name of the function that does what you want ("stricmp"), or are you trying to re-invent the wheel?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by asteroid1122 View Post
    unfortunately that does not work or I did not do it well
    please help me with the code thanx
    Here are the steps:
    1. Input the two strings using fgets like
    Code:
    fgets(s1,sizeof(s1),stdin);
    2. Using loops convert each char of s2(considering it's the one which is uppercase) to lowercase. You will have to use tolower(s2[i]) function in ctype.h
    3. Use strcmp to compare the strings.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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