How do I properly compare strings?
up vote
145
down vote
favorite
I am trying to get a program to let a user enter a word or character, store it, and then print it until the user types it again, exiting the program. My code looks like this:
#include <stdio.h>
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!nPlease enter a word or character:n");
gets(input);
printf("I will now repeat this until you type it back to me.n");
while (check != input)
{
printf("%sn", input);
gets(check);
}
printf("Good bye!");
return 0;
}
The problem is that I keep getting the printing of the input string, even when the input by the user (check) matches the original (input). Am I comparing the two incorrectly?
c string strcmp
add a comment |
up vote
145
down vote
favorite
I am trying to get a program to let a user enter a word or character, store it, and then print it until the user types it again, exiting the program. My code looks like this:
#include <stdio.h>
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!nPlease enter a word or character:n");
gets(input);
printf("I will now repeat this until you type it back to me.n");
while (check != input)
{
printf("%sn", input);
gets(check);
}
printf("Good bye!");
return 0;
}
The problem is that I keep getting the printing of the input string, even when the input by the user (check) matches the original (input). Am I comparing the two incorrectly?
c string strcmp
8
gets( )was removed from the standard. Usefgets( )instead.
– stackptr
Jan 29 '15 at 2:31
Note that this answer to Why doesstrcmp()return zero when its inputs are equal explains how to compare strings for equality, inequality, less than, greater than, less than or equal, and greater than or equal. Not all string comparisons are for equality. Case sensitive comparisons are different again; other special comparisons (dictionary order, for example) require more specialized comparators, and there are regexes for still more complex comparisons.
– Jonathan Leffler
Nov 22 '16 at 22:44
Note too that there is an essentially duplicate question How do I check if a value matches a string that was asked years before this.
– Jonathan Leffler
Feb 10 '17 at 5:24
add a comment |
up vote
145
down vote
favorite
up vote
145
down vote
favorite
I am trying to get a program to let a user enter a word or character, store it, and then print it until the user types it again, exiting the program. My code looks like this:
#include <stdio.h>
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!nPlease enter a word or character:n");
gets(input);
printf("I will now repeat this until you type it back to me.n");
while (check != input)
{
printf("%sn", input);
gets(check);
}
printf("Good bye!");
return 0;
}
The problem is that I keep getting the printing of the input string, even when the input by the user (check) matches the original (input). Am I comparing the two incorrectly?
c string strcmp
I am trying to get a program to let a user enter a word or character, store it, and then print it until the user types it again, exiting the program. My code looks like this:
#include <stdio.h>
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!nPlease enter a word or character:n");
gets(input);
printf("I will now repeat this until you type it back to me.n");
while (check != input)
{
printf("%sn", input);
gets(check);
}
printf("Good bye!");
return 0;
}
The problem is that I keep getting the printing of the input string, even when the input by the user (check) matches the original (input). Am I comparing the two incorrectly?
c string strcmp
c string strcmp
edited May 22 '17 at 9:27
Lundin
105k17154258
105k17154258
asked Nov 4 '11 at 2:22
nmagerko
2,76462963
2,76462963
8
gets( )was removed from the standard. Usefgets( )instead.
– stackptr
Jan 29 '15 at 2:31
Note that this answer to Why doesstrcmp()return zero when its inputs are equal explains how to compare strings for equality, inequality, less than, greater than, less than or equal, and greater than or equal. Not all string comparisons are for equality. Case sensitive comparisons are different again; other special comparisons (dictionary order, for example) require more specialized comparators, and there are regexes for still more complex comparisons.
– Jonathan Leffler
Nov 22 '16 at 22:44
Note too that there is an essentially duplicate question How do I check if a value matches a string that was asked years before this.
– Jonathan Leffler
Feb 10 '17 at 5:24
add a comment |
8
gets( )was removed from the standard. Usefgets( )instead.
– stackptr
Jan 29 '15 at 2:31
Note that this answer to Why doesstrcmp()return zero when its inputs are equal explains how to compare strings for equality, inequality, less than, greater than, less than or equal, and greater than or equal. Not all string comparisons are for equality. Case sensitive comparisons are different again; other special comparisons (dictionary order, for example) require more specialized comparators, and there are regexes for still more complex comparisons.
– Jonathan Leffler
Nov 22 '16 at 22:44
Note too that there is an essentially duplicate question How do I check if a value matches a string that was asked years before this.
– Jonathan Leffler
Feb 10 '17 at 5:24
8
8
gets( ) was removed from the standard. Use fgets( ) instead.– stackptr
Jan 29 '15 at 2:31
gets( ) was removed from the standard. Use fgets( ) instead.– stackptr
Jan 29 '15 at 2:31
Note that this answer to Why does
strcmp() return zero when its inputs are equal explains how to compare strings for equality, inequality, less than, greater than, less than or equal, and greater than or equal. Not all string comparisons are for equality. Case sensitive comparisons are different again; other special comparisons (dictionary order, for example) require more specialized comparators, and there are regexes for still more complex comparisons.– Jonathan Leffler
Nov 22 '16 at 22:44
Note that this answer to Why does
strcmp() return zero when its inputs are equal explains how to compare strings for equality, inequality, less than, greater than, less than or equal, and greater than or equal. Not all string comparisons are for equality. Case sensitive comparisons are different again; other special comparisons (dictionary order, for example) require more specialized comparators, and there are regexes for still more complex comparisons.– Jonathan Leffler
Nov 22 '16 at 22:44
Note too that there is an essentially duplicate question How do I check if a value matches a string that was asked years before this.
– Jonathan Leffler
Feb 10 '17 at 5:24
Note too that there is an essentially duplicate question How do I check if a value matches a string that was asked years before this.
– Jonathan Leffler
Feb 10 '17 at 5:24
add a comment |
7 Answers
7
active
oldest
votes
up vote
225
down vote
accepted
You can't (usefully) compare strings using != or ==, you need to use strcmp:
while (strcmp(check,input) != 0)
The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings themselves.
8
the same in java,which may just compare with the address.
– Telerik
Sep 6 '14 at 16:29
18
Writingwhile (strcmp(check, input))is sufficient and is considered good practice.
– The Peaceful Coder
Jun 28 '15 at 15:53
know more...codificare.in/codes/c/…
– chanu panwar
Jun 25 '16 at 9:55
3
It is safer to use strncmp! Don't want a buffer overflow!
– Floam
Nov 10 '17 at 18:36
add a comment |
up vote
30
down vote
Ok a few things: gets is unsafe and should be replaced with fgets(input, sizeof(input), stdin) so that you don't get a buffer overflow.
Next, to compare strings, you must use strcmp, where a return value of 0 indicates that the two strings match. Using the equality operators (ie. !=) compares the address of the two strings, as opposed to the individual chars inside them.
And also note that, while in this example it won't cause a problem, fgets stores the newline character, 'n' in the buffers also; gets() does not. If you compared the user input from fgets() to a string literal such as "abc" it would never match (unless the buffer was too small so that the 'n' wouldn't fit in it).
EDIT: and beaten by the super fast Mysticial once again.
I understand what you are saying, but this is just a learning example. I will not be using it for any important reason.
– nmagerko
Nov 4 '11 at 2:30
7
@nmagerko yeah I understand. It's always important to realise that though.
– AusCBloke
Nov 4 '11 at 2:32
2
The first argument offgetsshould be achar*. Butstdinis aFILE*
– Cool Guy
Feb 11 '15 at 11:08
add a comment |
up vote
6
down vote
You can't compare arrays directly like this
array1==array2
You should compare them char-by-char; for this you can use a function and return a boolean (True:1, False:0) value. Then you can use it in the test condition of the while loop.
Try this:
#include <stdio.h>
int checker(char input,char check);
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!nPlease enter a word or character:n");
scanf("%s",input);
printf("I will now repeat this until you type it back to me.n");
scanf("%s",check);
while (!checker(input,check))
{
printf("%sn", input);
scanf("%s",check);
}
printf("Good bye!");
return 0;
}
int checker(char input,char check)
{
int i,result=1;
for(i=0; input[i]!='' || check[i]!=''; i++) {
if(input[i] != check[i]) {
result=0;
break;
}
}
return result;
}
1
Could you please add more details about your solution?
– abarisone
Apr 6 '15 at 9:48
i edited my post and added some explanations
– mugetsu
Apr 6 '15 at 9:59
yes this is replacement for strcmp function and solition without using string.h header @Jongware
– mugetsu
Apr 6 '15 at 10:02
2
This does not work. Whencheckerfinds''in one of the strings, it does not check the another string for''. The function returns1("equal") even if one string is only prefix of the other one (for example,"foo"and"foobar").
– lukasrozs
Oct 6 '17 at 14:16
1
I would use||instead of&&.
– lukasrozs
Oct 6 '17 at 14:28
add a comment |
up vote
4
down vote
Use strcmp.
This is in string.h library, and is very popular. strcmp return 0 if the strings are equal. See this for an better explanation of what strcmp returns.
Basically, you have to do:
while (strcmp(check,input) != 0)
or
while (!strcmp(check,input))
or
while (strcmp(check,input))
You can check this, a tutorial on strcmp.
add a comment |
up vote
1
down vote
Whenever you are trying to compare the strings, compare them with respect to each character. For this you can use built in string function called strcmp(input1,input2); and you should use the header file called #include<string.h>
Try this code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s="STACKOVERFLOW";
char s1[200];
printf("Enter the string to be checkedn");//enter the input string
scanf("%s",s1);
if(strcmp(s,s1)==0)//compare both the strings
{
printf("Both the Strings matchn");
}
else
{
printf("Entered String does not matchn");
}
system("pause");
}
Damn, you really need some spaces
– jv110
Aug 22 '17 at 20:17
add a comment |
up vote
0
down vote
Unfortunately you can't use strcmp from <cstring> because it is a C++ header and you specifically said it is for a C application. I had the same problem, so I had to write my own function that implements strcmp:
int strcmp(char input, char check)
{
for (int i = 0;; i++)
{
if (input[i] == '' && check[i] == '')
{
break;
}
else if (input[i] == '' && check[i] != '')
{
return 1;
}
else if (input[i] != '' && check[i] == '')
{
return -1;
}
else if (input[i] > check[i])
{
return 1;
}
else if (input[i] < check[i])
{
return -1;
}
else
{
// characters are the same - continue and check next
}
}
return 0;
}
I hope this serves you well.
4
It's<string.h>in C. No need to reimplement it.
– mk12
Oct 31 '15 at 16:52
Oh, thanks. I guess I will just use that instead. Anyway, here is an example of how to compare arrays... :)
– Steztric
Nov 4 '15 at 10:18
1
It'd be good to edit your answer to reflect the info in comments; and also call your function something different (reusing names from standard library causes undefined behaviour)
– M.M
Nov 11 '15 at 0:38
add a comment |
up vote
0
down vote
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],s2[50];
printf("Enter the character of strings: ");
gets(s1);
printf("nEnter different character of string to repeat: n");
while(strcmp(s1,s2))
{
printf("%sn",s1);
gets(s2);
}
return 0;
}
This is very simple solution in which you will get your output as you want.
add a comment |
protected by Mysticial Jun 2 '16 at 19:03
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
225
down vote
accepted
You can't (usefully) compare strings using != or ==, you need to use strcmp:
while (strcmp(check,input) != 0)
The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings themselves.
8
the same in java,which may just compare with the address.
– Telerik
Sep 6 '14 at 16:29
18
Writingwhile (strcmp(check, input))is sufficient and is considered good practice.
– The Peaceful Coder
Jun 28 '15 at 15:53
know more...codificare.in/codes/c/…
– chanu panwar
Jun 25 '16 at 9:55
3
It is safer to use strncmp! Don't want a buffer overflow!
– Floam
Nov 10 '17 at 18:36
add a comment |
up vote
225
down vote
accepted
You can't (usefully) compare strings using != or ==, you need to use strcmp:
while (strcmp(check,input) != 0)
The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings themselves.
8
the same in java,which may just compare with the address.
– Telerik
Sep 6 '14 at 16:29
18
Writingwhile (strcmp(check, input))is sufficient and is considered good practice.
– The Peaceful Coder
Jun 28 '15 at 15:53
know more...codificare.in/codes/c/…
– chanu panwar
Jun 25 '16 at 9:55
3
It is safer to use strncmp! Don't want a buffer overflow!
– Floam
Nov 10 '17 at 18:36
add a comment |
up vote
225
down vote
accepted
up vote
225
down vote
accepted
You can't (usefully) compare strings using != or ==, you need to use strcmp:
while (strcmp(check,input) != 0)
The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings themselves.
You can't (usefully) compare strings using != or ==, you need to use strcmp:
while (strcmp(check,input) != 0)
The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings themselves.
edited Sep 20 '15 at 5:57
Jonathan Leffler
556k886621015
556k886621015
answered Nov 4 '11 at 2:24
Mysticial
378k39289298
378k39289298
8
the same in java,which may just compare with the address.
– Telerik
Sep 6 '14 at 16:29
18
Writingwhile (strcmp(check, input))is sufficient and is considered good practice.
– The Peaceful Coder
Jun 28 '15 at 15:53
know more...codificare.in/codes/c/…
– chanu panwar
Jun 25 '16 at 9:55
3
It is safer to use strncmp! Don't want a buffer overflow!
– Floam
Nov 10 '17 at 18:36
add a comment |
8
the same in java,which may just compare with the address.
– Telerik
Sep 6 '14 at 16:29
18
Writingwhile (strcmp(check, input))is sufficient and is considered good practice.
– The Peaceful Coder
Jun 28 '15 at 15:53
know more...codificare.in/codes/c/…
– chanu panwar
Jun 25 '16 at 9:55
3
It is safer to use strncmp! Don't want a buffer overflow!
– Floam
Nov 10 '17 at 18:36
8
8
the same in java,which may just compare with the address.
– Telerik
Sep 6 '14 at 16:29
the same in java,which may just compare with the address.
– Telerik
Sep 6 '14 at 16:29
18
18
Writing
while (strcmp(check, input)) is sufficient and is considered good practice.– The Peaceful Coder
Jun 28 '15 at 15:53
Writing
while (strcmp(check, input)) is sufficient and is considered good practice.– The Peaceful Coder
Jun 28 '15 at 15:53
know more...codificare.in/codes/c/…
– chanu panwar
Jun 25 '16 at 9:55
know more...codificare.in/codes/c/…
– chanu panwar
Jun 25 '16 at 9:55
3
3
It is safer to use strncmp! Don't want a buffer overflow!
– Floam
Nov 10 '17 at 18:36
It is safer to use strncmp! Don't want a buffer overflow!
– Floam
Nov 10 '17 at 18:36
add a comment |
up vote
30
down vote
Ok a few things: gets is unsafe and should be replaced with fgets(input, sizeof(input), stdin) so that you don't get a buffer overflow.
Next, to compare strings, you must use strcmp, where a return value of 0 indicates that the two strings match. Using the equality operators (ie. !=) compares the address of the two strings, as opposed to the individual chars inside them.
And also note that, while in this example it won't cause a problem, fgets stores the newline character, 'n' in the buffers also; gets() does not. If you compared the user input from fgets() to a string literal such as "abc" it would never match (unless the buffer was too small so that the 'n' wouldn't fit in it).
EDIT: and beaten by the super fast Mysticial once again.
I understand what you are saying, but this is just a learning example. I will not be using it for any important reason.
– nmagerko
Nov 4 '11 at 2:30
7
@nmagerko yeah I understand. It's always important to realise that though.
– AusCBloke
Nov 4 '11 at 2:32
2
The first argument offgetsshould be achar*. Butstdinis aFILE*
– Cool Guy
Feb 11 '15 at 11:08
add a comment |
up vote
30
down vote
Ok a few things: gets is unsafe and should be replaced with fgets(input, sizeof(input), stdin) so that you don't get a buffer overflow.
Next, to compare strings, you must use strcmp, where a return value of 0 indicates that the two strings match. Using the equality operators (ie. !=) compares the address of the two strings, as opposed to the individual chars inside them.
And also note that, while in this example it won't cause a problem, fgets stores the newline character, 'n' in the buffers also; gets() does not. If you compared the user input from fgets() to a string literal such as "abc" it would never match (unless the buffer was too small so that the 'n' wouldn't fit in it).
EDIT: and beaten by the super fast Mysticial once again.
I understand what you are saying, but this is just a learning example. I will not be using it for any important reason.
– nmagerko
Nov 4 '11 at 2:30
7
@nmagerko yeah I understand. It's always important to realise that though.
– AusCBloke
Nov 4 '11 at 2:32
2
The first argument offgetsshould be achar*. Butstdinis aFILE*
– Cool Guy
Feb 11 '15 at 11:08
add a comment |
up vote
30
down vote
up vote
30
down vote
Ok a few things: gets is unsafe and should be replaced with fgets(input, sizeof(input), stdin) so that you don't get a buffer overflow.
Next, to compare strings, you must use strcmp, where a return value of 0 indicates that the two strings match. Using the equality operators (ie. !=) compares the address of the two strings, as opposed to the individual chars inside them.
And also note that, while in this example it won't cause a problem, fgets stores the newline character, 'n' in the buffers also; gets() does not. If you compared the user input from fgets() to a string literal such as "abc" it would never match (unless the buffer was too small so that the 'n' wouldn't fit in it).
EDIT: and beaten by the super fast Mysticial once again.
Ok a few things: gets is unsafe and should be replaced with fgets(input, sizeof(input), stdin) so that you don't get a buffer overflow.
Next, to compare strings, you must use strcmp, where a return value of 0 indicates that the two strings match. Using the equality operators (ie. !=) compares the address of the two strings, as opposed to the individual chars inside them.
And also note that, while in this example it won't cause a problem, fgets stores the newline character, 'n' in the buffers also; gets() does not. If you compared the user input from fgets() to a string literal such as "abc" it would never match (unless the buffer was too small so that the 'n' wouldn't fit in it).
EDIT: and beaten by the super fast Mysticial once again.
edited Feb 10 '17 at 5:10
Jonathan Leffler
556k886621015
556k886621015
answered Nov 4 '11 at 2:29
AusCBloke
14.7k53443
14.7k53443
I understand what you are saying, but this is just a learning example. I will not be using it for any important reason.
– nmagerko
Nov 4 '11 at 2:30
7
@nmagerko yeah I understand. It's always important to realise that though.
– AusCBloke
Nov 4 '11 at 2:32
2
The first argument offgetsshould be achar*. Butstdinis aFILE*
– Cool Guy
Feb 11 '15 at 11:08
add a comment |
I understand what you are saying, but this is just a learning example. I will not be using it for any important reason.
– nmagerko
Nov 4 '11 at 2:30
7
@nmagerko yeah I understand. It's always important to realise that though.
– AusCBloke
Nov 4 '11 at 2:32
2
The first argument offgetsshould be achar*. Butstdinis aFILE*
– Cool Guy
Feb 11 '15 at 11:08
I understand what you are saying, but this is just a learning example. I will not be using it for any important reason.
– nmagerko
Nov 4 '11 at 2:30
I understand what you are saying, but this is just a learning example. I will not be using it for any important reason.
– nmagerko
Nov 4 '11 at 2:30
7
7
@nmagerko yeah I understand. It's always important to realise that though.
– AusCBloke
Nov 4 '11 at 2:32
@nmagerko yeah I understand. It's always important to realise that though.
– AusCBloke
Nov 4 '11 at 2:32
2
2
The first argument of
fgets should be a char*. But stdin is a FILE*– Cool Guy
Feb 11 '15 at 11:08
The first argument of
fgets should be a char*. But stdin is a FILE*– Cool Guy
Feb 11 '15 at 11:08
add a comment |
up vote
6
down vote
You can't compare arrays directly like this
array1==array2
You should compare them char-by-char; for this you can use a function and return a boolean (True:1, False:0) value. Then you can use it in the test condition of the while loop.
Try this:
#include <stdio.h>
int checker(char input,char check);
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!nPlease enter a word or character:n");
scanf("%s",input);
printf("I will now repeat this until you type it back to me.n");
scanf("%s",check);
while (!checker(input,check))
{
printf("%sn", input);
scanf("%s",check);
}
printf("Good bye!");
return 0;
}
int checker(char input,char check)
{
int i,result=1;
for(i=0; input[i]!='' || check[i]!=''; i++) {
if(input[i] != check[i]) {
result=0;
break;
}
}
return result;
}
1
Could you please add more details about your solution?
– abarisone
Apr 6 '15 at 9:48
i edited my post and added some explanations
– mugetsu
Apr 6 '15 at 9:59
yes this is replacement for strcmp function and solition without using string.h header @Jongware
– mugetsu
Apr 6 '15 at 10:02
2
This does not work. Whencheckerfinds''in one of the strings, it does not check the another string for''. The function returns1("equal") even if one string is only prefix of the other one (for example,"foo"and"foobar").
– lukasrozs
Oct 6 '17 at 14:16
1
I would use||instead of&&.
– lukasrozs
Oct 6 '17 at 14:28
add a comment |
up vote
6
down vote
You can't compare arrays directly like this
array1==array2
You should compare them char-by-char; for this you can use a function and return a boolean (True:1, False:0) value. Then you can use it in the test condition of the while loop.
Try this:
#include <stdio.h>
int checker(char input,char check);
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!nPlease enter a word or character:n");
scanf("%s",input);
printf("I will now repeat this until you type it back to me.n");
scanf("%s",check);
while (!checker(input,check))
{
printf("%sn", input);
scanf("%s",check);
}
printf("Good bye!");
return 0;
}
int checker(char input,char check)
{
int i,result=1;
for(i=0; input[i]!='' || check[i]!=''; i++) {
if(input[i] != check[i]) {
result=0;
break;
}
}
return result;
}
1
Could you please add more details about your solution?
– abarisone
Apr 6 '15 at 9:48
i edited my post and added some explanations
– mugetsu
Apr 6 '15 at 9:59
yes this is replacement for strcmp function and solition without using string.h header @Jongware
– mugetsu
Apr 6 '15 at 10:02
2
This does not work. Whencheckerfinds''in one of the strings, it does not check the another string for''. The function returns1("equal") even if one string is only prefix of the other one (for example,"foo"and"foobar").
– lukasrozs
Oct 6 '17 at 14:16
1
I would use||instead of&&.
– lukasrozs
Oct 6 '17 at 14:28
add a comment |
up vote
6
down vote
up vote
6
down vote
You can't compare arrays directly like this
array1==array2
You should compare them char-by-char; for this you can use a function and return a boolean (True:1, False:0) value. Then you can use it in the test condition of the while loop.
Try this:
#include <stdio.h>
int checker(char input,char check);
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!nPlease enter a word or character:n");
scanf("%s",input);
printf("I will now repeat this until you type it back to me.n");
scanf("%s",check);
while (!checker(input,check))
{
printf("%sn", input);
scanf("%s",check);
}
printf("Good bye!");
return 0;
}
int checker(char input,char check)
{
int i,result=1;
for(i=0; input[i]!='' || check[i]!=''; i++) {
if(input[i] != check[i]) {
result=0;
break;
}
}
return result;
}
You can't compare arrays directly like this
array1==array2
You should compare them char-by-char; for this you can use a function and return a boolean (True:1, False:0) value. Then you can use it in the test condition of the while loop.
Try this:
#include <stdio.h>
int checker(char input,char check);
int main()
{
char input[40];
char check[40];
int i=0;
printf("Hello!nPlease enter a word or character:n");
scanf("%s",input);
printf("I will now repeat this until you type it back to me.n");
scanf("%s",check);
while (!checker(input,check))
{
printf("%sn", input);
scanf("%s",check);
}
printf("Good bye!");
return 0;
}
int checker(char input,char check)
{
int i,result=1;
for(i=0; input[i]!='' || check[i]!=''; i++) {
if(input[i] != check[i]) {
result=0;
break;
}
}
return result;
}
edited Dec 28 '17 at 0:56
Shubham
1,90221327
1,90221327
answered Apr 6 '15 at 9:46
mugetsu
105110
105110
1
Could you please add more details about your solution?
– abarisone
Apr 6 '15 at 9:48
i edited my post and added some explanations
– mugetsu
Apr 6 '15 at 9:59
yes this is replacement for strcmp function and solition without using string.h header @Jongware
– mugetsu
Apr 6 '15 at 10:02
2
This does not work. Whencheckerfinds''in one of the strings, it does not check the another string for''. The function returns1("equal") even if one string is only prefix of the other one (for example,"foo"and"foobar").
– lukasrozs
Oct 6 '17 at 14:16
1
I would use||instead of&&.
– lukasrozs
Oct 6 '17 at 14:28
add a comment |
1
Could you please add more details about your solution?
– abarisone
Apr 6 '15 at 9:48
i edited my post and added some explanations
– mugetsu
Apr 6 '15 at 9:59
yes this is replacement for strcmp function and solition without using string.h header @Jongware
– mugetsu
Apr 6 '15 at 10:02
2
This does not work. Whencheckerfinds''in one of the strings, it does not check the another string for''. The function returns1("equal") even if one string is only prefix of the other one (for example,"foo"and"foobar").
– lukasrozs
Oct 6 '17 at 14:16
1
I would use||instead of&&.
– lukasrozs
Oct 6 '17 at 14:28
1
1
Could you please add more details about your solution?
– abarisone
Apr 6 '15 at 9:48
Could you please add more details about your solution?
– abarisone
Apr 6 '15 at 9:48
i edited my post and added some explanations
– mugetsu
Apr 6 '15 at 9:59
i edited my post and added some explanations
– mugetsu
Apr 6 '15 at 9:59
yes this is replacement for strcmp function and solition without using string.h header @Jongware
– mugetsu
Apr 6 '15 at 10:02
yes this is replacement for strcmp function and solition without using string.h header @Jongware
– mugetsu
Apr 6 '15 at 10:02
2
2
This does not work. When
checker finds '' in one of the strings, it does not check the another string for ''. The function returns 1 ("equal") even if one string is only prefix of the other one (for example, "foo" and "foobar").– lukasrozs
Oct 6 '17 at 14:16
This does not work. When
checker finds '' in one of the strings, it does not check the another string for ''. The function returns 1 ("equal") even if one string is only prefix of the other one (for example, "foo" and "foobar").– lukasrozs
Oct 6 '17 at 14:16
1
1
I would use
|| instead of &&.– lukasrozs
Oct 6 '17 at 14:28
I would use
|| instead of &&.– lukasrozs
Oct 6 '17 at 14:28
add a comment |
up vote
4
down vote
Use strcmp.
This is in string.h library, and is very popular. strcmp return 0 if the strings are equal. See this for an better explanation of what strcmp returns.
Basically, you have to do:
while (strcmp(check,input) != 0)
or
while (!strcmp(check,input))
or
while (strcmp(check,input))
You can check this, a tutorial on strcmp.
add a comment |
up vote
4
down vote
Use strcmp.
This is in string.h library, and is very popular. strcmp return 0 if the strings are equal. See this for an better explanation of what strcmp returns.
Basically, you have to do:
while (strcmp(check,input) != 0)
or
while (!strcmp(check,input))
or
while (strcmp(check,input))
You can check this, a tutorial on strcmp.
add a comment |
up vote
4
down vote
up vote
4
down vote
Use strcmp.
This is in string.h library, and is very popular. strcmp return 0 if the strings are equal. See this for an better explanation of what strcmp returns.
Basically, you have to do:
while (strcmp(check,input) != 0)
or
while (!strcmp(check,input))
or
while (strcmp(check,input))
You can check this, a tutorial on strcmp.
Use strcmp.
This is in string.h library, and is very popular. strcmp return 0 if the strings are equal. See this for an better explanation of what strcmp returns.
Basically, you have to do:
while (strcmp(check,input) != 0)
or
while (!strcmp(check,input))
or
while (strcmp(check,input))
You can check this, a tutorial on strcmp.
edited May 23 '17 at 11:47
Community♦
11
11
answered Feb 13 '16 at 7:14
fortunate_man
3,73993355
3,73993355
add a comment |
add a comment |
up vote
1
down vote
Whenever you are trying to compare the strings, compare them with respect to each character. For this you can use built in string function called strcmp(input1,input2); and you should use the header file called #include<string.h>
Try this code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s="STACKOVERFLOW";
char s1[200];
printf("Enter the string to be checkedn");//enter the input string
scanf("%s",s1);
if(strcmp(s,s1)==0)//compare both the strings
{
printf("Both the Strings matchn");
}
else
{
printf("Entered String does not matchn");
}
system("pause");
}
Damn, you really need some spaces
– jv110
Aug 22 '17 at 20:17
add a comment |
up vote
1
down vote
Whenever you are trying to compare the strings, compare them with respect to each character. For this you can use built in string function called strcmp(input1,input2); and you should use the header file called #include<string.h>
Try this code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s="STACKOVERFLOW";
char s1[200];
printf("Enter the string to be checkedn");//enter the input string
scanf("%s",s1);
if(strcmp(s,s1)==0)//compare both the strings
{
printf("Both the Strings matchn");
}
else
{
printf("Entered String does not matchn");
}
system("pause");
}
Damn, you really need some spaces
– jv110
Aug 22 '17 at 20:17
add a comment |
up vote
1
down vote
up vote
1
down vote
Whenever you are trying to compare the strings, compare them with respect to each character. For this you can use built in string function called strcmp(input1,input2); and you should use the header file called #include<string.h>
Try this code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s="STACKOVERFLOW";
char s1[200];
printf("Enter the string to be checkedn");//enter the input string
scanf("%s",s1);
if(strcmp(s,s1)==0)//compare both the strings
{
printf("Both the Strings matchn");
}
else
{
printf("Entered String does not matchn");
}
system("pause");
}
Whenever you are trying to compare the strings, compare them with respect to each character. For this you can use built in string function called strcmp(input1,input2); and you should use the header file called #include<string.h>
Try this code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s="STACKOVERFLOW";
char s1[200];
printf("Enter the string to be checkedn");//enter the input string
scanf("%s",s1);
if(strcmp(s,s1)==0)//compare both the strings
{
printf("Both the Strings matchn");
}
else
{
printf("Entered String does not matchn");
}
system("pause");
}
edited Mar 3 '16 at 19:13
StephenTG
2,19031934
2,19031934
answered Mar 3 '16 at 18:23
Vishwanath Tallalli
113
113
Damn, you really need some spaces
– jv110
Aug 22 '17 at 20:17
add a comment |
Damn, you really need some spaces
– jv110
Aug 22 '17 at 20:17
Damn, you really need some spaces
– jv110
Aug 22 '17 at 20:17
Damn, you really need some spaces
– jv110
Aug 22 '17 at 20:17
add a comment |
up vote
0
down vote
Unfortunately you can't use strcmp from <cstring> because it is a C++ header and you specifically said it is for a C application. I had the same problem, so I had to write my own function that implements strcmp:
int strcmp(char input, char check)
{
for (int i = 0;; i++)
{
if (input[i] == '' && check[i] == '')
{
break;
}
else if (input[i] == '' && check[i] != '')
{
return 1;
}
else if (input[i] != '' && check[i] == '')
{
return -1;
}
else if (input[i] > check[i])
{
return 1;
}
else if (input[i] < check[i])
{
return -1;
}
else
{
// characters are the same - continue and check next
}
}
return 0;
}
I hope this serves you well.
4
It's<string.h>in C. No need to reimplement it.
– mk12
Oct 31 '15 at 16:52
Oh, thanks. I guess I will just use that instead. Anyway, here is an example of how to compare arrays... :)
– Steztric
Nov 4 '15 at 10:18
1
It'd be good to edit your answer to reflect the info in comments; and also call your function something different (reusing names from standard library causes undefined behaviour)
– M.M
Nov 11 '15 at 0:38
add a comment |
up vote
0
down vote
Unfortunately you can't use strcmp from <cstring> because it is a C++ header and you specifically said it is for a C application. I had the same problem, so I had to write my own function that implements strcmp:
int strcmp(char input, char check)
{
for (int i = 0;; i++)
{
if (input[i] == '' && check[i] == '')
{
break;
}
else if (input[i] == '' && check[i] != '')
{
return 1;
}
else if (input[i] != '' && check[i] == '')
{
return -1;
}
else if (input[i] > check[i])
{
return 1;
}
else if (input[i] < check[i])
{
return -1;
}
else
{
// characters are the same - continue and check next
}
}
return 0;
}
I hope this serves you well.
4
It's<string.h>in C. No need to reimplement it.
– mk12
Oct 31 '15 at 16:52
Oh, thanks. I guess I will just use that instead. Anyway, here is an example of how to compare arrays... :)
– Steztric
Nov 4 '15 at 10:18
1
It'd be good to edit your answer to reflect the info in comments; and also call your function something different (reusing names from standard library causes undefined behaviour)
– M.M
Nov 11 '15 at 0:38
add a comment |
up vote
0
down vote
up vote
0
down vote
Unfortunately you can't use strcmp from <cstring> because it is a C++ header and you specifically said it is for a C application. I had the same problem, so I had to write my own function that implements strcmp:
int strcmp(char input, char check)
{
for (int i = 0;; i++)
{
if (input[i] == '' && check[i] == '')
{
break;
}
else if (input[i] == '' && check[i] != '')
{
return 1;
}
else if (input[i] != '' && check[i] == '')
{
return -1;
}
else if (input[i] > check[i])
{
return 1;
}
else if (input[i] < check[i])
{
return -1;
}
else
{
// characters are the same - continue and check next
}
}
return 0;
}
I hope this serves you well.
Unfortunately you can't use strcmp from <cstring> because it is a C++ header and you specifically said it is for a C application. I had the same problem, so I had to write my own function that implements strcmp:
int strcmp(char input, char check)
{
for (int i = 0;; i++)
{
if (input[i] == '' && check[i] == '')
{
break;
}
else if (input[i] == '' && check[i] != '')
{
return 1;
}
else if (input[i] != '' && check[i] == '')
{
return -1;
}
else if (input[i] > check[i])
{
return 1;
}
else if (input[i] < check[i])
{
return -1;
}
else
{
// characters are the same - continue and check next
}
}
return 0;
}
I hope this serves you well.
answered Aug 28 '15 at 8:40
Steztric
1,61211532
1,61211532
4
It's<string.h>in C. No need to reimplement it.
– mk12
Oct 31 '15 at 16:52
Oh, thanks. I guess I will just use that instead. Anyway, here is an example of how to compare arrays... :)
– Steztric
Nov 4 '15 at 10:18
1
It'd be good to edit your answer to reflect the info in comments; and also call your function something different (reusing names from standard library causes undefined behaviour)
– M.M
Nov 11 '15 at 0:38
add a comment |
4
It's<string.h>in C. No need to reimplement it.
– mk12
Oct 31 '15 at 16:52
Oh, thanks. I guess I will just use that instead. Anyway, here is an example of how to compare arrays... :)
– Steztric
Nov 4 '15 at 10:18
1
It'd be good to edit your answer to reflect the info in comments; and also call your function something different (reusing names from standard library causes undefined behaviour)
– M.M
Nov 11 '15 at 0:38
4
4
It's
<string.h> in C. No need to reimplement it.– mk12
Oct 31 '15 at 16:52
It's
<string.h> in C. No need to reimplement it.– mk12
Oct 31 '15 at 16:52
Oh, thanks. I guess I will just use that instead. Anyway, here is an example of how to compare arrays... :)
– Steztric
Nov 4 '15 at 10:18
Oh, thanks. I guess I will just use that instead. Anyway, here is an example of how to compare arrays... :)
– Steztric
Nov 4 '15 at 10:18
1
1
It'd be good to edit your answer to reflect the info in comments; and also call your function something different (reusing names from standard library causes undefined behaviour)
– M.M
Nov 11 '15 at 0:38
It'd be good to edit your answer to reflect the info in comments; and also call your function something different (reusing names from standard library causes undefined behaviour)
– M.M
Nov 11 '15 at 0:38
add a comment |
up vote
0
down vote
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],s2[50];
printf("Enter the character of strings: ");
gets(s1);
printf("nEnter different character of string to repeat: n");
while(strcmp(s1,s2))
{
printf("%sn",s1);
gets(s2);
}
return 0;
}
This is very simple solution in which you will get your output as you want.
add a comment |
up vote
0
down vote
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],s2[50];
printf("Enter the character of strings: ");
gets(s1);
printf("nEnter different character of string to repeat: n");
while(strcmp(s1,s2))
{
printf("%sn",s1);
gets(s2);
}
return 0;
}
This is very simple solution in which you will get your output as you want.
add a comment |
up vote
0
down vote
up vote
0
down vote
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],s2[50];
printf("Enter the character of strings: ");
gets(s1);
printf("nEnter different character of string to repeat: n");
while(strcmp(s1,s2))
{
printf("%sn",s1);
gets(s2);
}
return 0;
}
This is very simple solution in which you will get your output as you want.
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],s2[50];
printf("Enter the character of strings: ");
gets(s1);
printf("nEnter different character of string to repeat: n");
while(strcmp(s1,s2))
{
printf("%sn",s1);
gets(s2);
}
return 0;
}
This is very simple solution in which you will get your output as you want.
answered Mar 23 at 7:26
Rupani T D
114
114
add a comment |
add a comment |
protected by Mysticial Jun 2 '16 at 19:03
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
8
gets( )was removed from the standard. Usefgets( )instead.– stackptr
Jan 29 '15 at 2:31
Note that this answer to Why does
strcmp()return zero when its inputs are equal explains how to compare strings for equality, inequality, less than, greater than, less than or equal, and greater than or equal. Not all string comparisons are for equality. Case sensitive comparisons are different again; other special comparisons (dictionary order, for example) require more specialized comparators, and there are regexes for still more complex comparisons.– Jonathan Leffler
Nov 22 '16 at 22:44
Note too that there is an essentially duplicate question How do I check if a value matches a string that was asked years before this.
– Jonathan Leffler
Feb 10 '17 at 5:24