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.
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.
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.
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: 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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
mouse666666, looks like another would disagree. I'll compare it to mine. Thank you for your help.
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');
Instead of using scanf, it would be easier to use getchar(), e.g.,
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.Code:c = getchar();
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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: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(); }
Read, and learn:
- cprogramming.com FAQ - Why gets() is bad/buffer overflows
- comp.lang.c C-FAQ - Why does everyone say not to use gets()?
- cpwiki FAQ - gets
And finally, from man 3 gets (Linux):
Now to aplst3: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.
You had this:
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.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 */
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.
>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
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.
There are probably more, but I'm unfamiliar with getch and conio and such.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. }
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.
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.
> 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
Yeah I know there are only 10 red comments, I left 2 off to assist with further reading.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 */ }
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.