Thread: Comparing Strings

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Comparing Strings

    Write a program that uses function strcmp to compare two strings input by the user. The program should state whether the first string is less than, equal to or greater than the second string.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, you write a program that uses function strcmp to compare two strings input by the user. YOUR program should state whether the first string is less than, equal to, or greater than the second string.

    Seriously, who posts crap like this and expects someone to do it for them?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    here let me help you get started you will need something like this and ill leave the rest on to you
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    
    main()
    {
          char first[100],second[100];
          
          printf("enter the first string :");
          gets(first);
          printf("enter the second string :");
          gets(second);
          
         if(strcmp(first,second)==0);
         {
         
         printf("the strings are equal");
         }
         
       
         getch();
    
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    mouse666666: cut down your example to a correct example of the use of strcmp. Your whole program is horribly wrong, and you managed to get the most relevant part wrong as well.

    But others have already told you: wait for an attempt (or at least believable signs of an attempt) before you provide an example.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Wow - I counted 8 mistakes in mouse666666's attempt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    3

    Program

    mouse666666, looks like another would disagree. I'll compare it to mine. Thank you for your help.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    3
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
            int main(void)
    {
            char str1[25]; /*initialize string 1*/
            char str2[25]; /* initialize string 2*/
    
            char c;
    
            do
    {
        printf("Enter the first string:\n");
        scanf("%s", &str1); /* reads string 1*/
    
        printf("Enter the second string:\n");
        scanf("%s",&str2); /* reads string 2*/
    
            if(strcmp(str1, str2) >= 1) { /* compare strings if first string is greater than second */
        printf("The first string is greater than the second string\n%s is greater than %s\n", str1, str2);
    }
            if(strcmp(str1, str2) <= -1){ /* compare strings if first string is less than second */
        printf("The first string is less than the second string\n%s is less than %s\n", str1, str2);
    }
            if(strcmp(str1, str2) == 0){ /* compare strings if the strings are equal */
        printf("The first string is equal to the second string\n%s is equal to %s\n", str1, str2);
    }
    
        printf( "Would you like another comparison? press y to continue:\n" );
        scanf( "%s",& c );
    }       while(c=='y'||c=='Y');
    
            return 0;
    
    {
    
    
    Instead of what I have, should the ending of the program look like this:
    
    Is my error at the end when asking the user if they want to enter another comparison?
    
    Another person told me to make sure to make the 'c' variable a character array of say 20 or so and then just compare the first character of the string. Like so:
    
    
    char c[20];  
     
    /* code here */ 
     
    scanf( "%s",&c );  
    }while(c[0]=='y'||c[0]=='Y');

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of using scanf, it would be easier to use getchar(), e.g.,
    Code:
    c = getchar();
    Alternatively, change %s to %c: the problem is now that the format specifier is that of a string, but you just want to read a char.

    A few other things to take note of:
    • Instead of comparing against 1 and -1, compare against 0, e.g., strcmp(str1, str2) > 0.
    • Store the result of strcmp in a variable.
    • Make use of else.
    • Indent your code more consistently.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    here let me help you get started you will need something like this and ill leave the rest on to you

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    
    main()
    {
          char first[100],second[100];
          
          printf("enter the first string :");
          gets(first);
          printf("enter the second string :");
          gets(second);
          
         if(strcmp(first,second)==0);
         {
         
         printf("the strings are equal");
         }
         
       
         getch();
    
    
    }
    mouse666666, I realise others have already called you on your errors here, but since I already took time to mention that gets is a bad idea (in this post) I would hate to see my work wasted. To recapitulate:


    Read, and learn:



    And finally, from man 3 gets (Linux):

    Never use gets(). Because it is impossible to tell without knowing
    the data in advance how many characters gets() will read, and
    because gets() will continue to store characters past the end of
    the buffer, it is extremely dangerous to use. It has been used to
    break computer security. Use fgets() instead.
    Now to aplst3:

    You had this:

    Code:
    int main(void)
    {
        char str1[25];              /*initialize string 1 */
        char str2[25];              /* initialize string 2 */
    
        char c;
    
        do {
            printf("Enter the first string:\n");
            scanf("%s", &str1);     /* reads string 1 */
    
            printf("Enter the second string:\n");
            scanf("%s", &str2);     /* reads string 2 */
    Your code is prone to the same sort of problems you would have if you had used gets instead of scanf. (scanf can be a troublesome way to get input) Generally speaking, scanf will read in text until it reaches a space, or a width specifier. You did not specify a width, and so the only thing that will stop your call to scanf is when the input is a space. What happens if a line of text with 500 characters (but no spaces) is entered? All of a sudden, your char array of 25 length is overrun. You need to limit user input.

    Now I realise these are just toy programs for the purpose of learning, but why learn inherently troublesome ways to code? Anyway, there is a much more rock solid way of getting a line of text from the user, and you can read all about it here.
    Last edited by kermit; 05-08-2010 at 06:30 AM.

  10. #10
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    >scanf("%s", &str1);
    Let's continue the quiz. Who's gonna tell me what's wrong with this?
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by GL.Sam View Post
    >scanf("%s", &str1);
    Let's continue the quiz. Who's gonna tell me what's wrong with this?
    I can see two things. Is that good enough?

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    since some of you told me there is 8 mistake. why dont you tell me the mistakes instead of saying WELL THERE IS 8 MISTAKES
    Last edited by mouse666666; 05-08-2010 at 09:17 PM.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by mouse666666 View Post
    since some of you told me there is 8 mistake. why dont you tell me the mistakes instead of saying WELL THERE IS 8 MISTAKES
    Code:
    #include <stdio.h>
    #include <stdlib.h> //Unnecessary, nothing included from here, not an error though
    #include <conio.h>
    #include <string.h>
    
    
    main() //main returns int
    {
          char first[100],second[100];
          
          printf("enter the first string :");
          gets(first); //don't use gets, for reasons already described
          printf("enter the second string :");
          gets(second);//same as above
          
         if(strcmp(first,second)==0); //Notice the semi colon here
         {
         
         printf("the strings are equal"); //The string will print regardless of the if statement
         }
         
       
         getch();
    
         //I would have said you forgot to return a value, but GL.Sam quoted the C standard about it in some other post and I'm inclined to believe him.
         //Still, I'd imagine it's good practice.
    }
    There are probably more, but I'm unfamiliar with getch and conio and such.

    Before posting code, you should be confident that it is correct so that you don't confuse people who are already confused (otherwise they wouldn't be posting).
    Last edited by DeadPlanet; 05-08-2010 at 09:38 PM.

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Because if you don't make an attempt at figuring them out, you will come back here whenever you run into the same "mistake".
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > why dont you tell me the mistakes instead of saying WELL THERE IS 8 MISTAKES
    Because it was a challenge for you to look at your code and try to figure out where you screwed up. Like for example even compiling and testing it perhaps (re: the ; on the if).



    I mis-counted (it was only a quick eyeball scan). It's up to 12 mistakes now
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h> /* unnecessary non-standard header */
    #include <string.h>
    
    
    main() /* implicit declaration of main retuning int */
    {
          char first[100],second[100];
          
          printf("enter the first string :"); /* \n or fflush(stdout) needed */
          gets(first);                        /* NEVER use gets() - see the FAQ */
          printf("enter the second string :");/* ditto above */
          gets(second);                       /* ditto above */
          
         if(strcmp(first,second)==0);         /* ; makes if statement null */
         {
         
         printf("the strings are equal");     /* ditto above */
         }
         
       
         getch();                             /* non-standard function - try getchar() */
    
        /* undefined return value from main */
    }
    Yeah I know there are only 10 red comments, I left 2 off to assist with further reading.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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, 10: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, 11:41 PM