How do I properly compare strings?











up vote
145
down vote

favorite
27












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?










share|improve this question




















  • 8




    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 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















up vote
145
down vote

favorite
27












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?










share|improve this question




















  • 8




    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 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













up vote
145
down vote

favorite
27









up vote
145
down vote

favorite
27






27





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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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. 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 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




    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 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












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.






share|improve this answer



















  • 8




    the same in java,which may just compare with the address.
    – Telerik
    Sep 6 '14 at 16:29






  • 18




    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






  • 3




    It is safer to use strncmp! Don't want a buffer overflow!
    – Floam
    Nov 10 '17 at 18:36


















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.






share|improve this answer























  • 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 of fgets should be a char*. But stdin is a FILE*
    – Cool Guy
    Feb 11 '15 at 11:08


















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;
}





share|improve this answer



















  • 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. 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




    I would use || instead of &&.
    – lukasrozs
    Oct 6 '17 at 14:28


















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.






share|improve this answer






























    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");
    }





    share|improve this answer























    • Damn, you really need some spaces
      – jv110
      Aug 22 '17 at 20:17


















    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.






    share|improve this answer

















    • 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


















    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.






    share|improve this answer




















      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.






      share|improve this answer



















      • 8




        the same in java,which may just compare with the address.
        – Telerik
        Sep 6 '14 at 16:29






      • 18




        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






      • 3




        It is safer to use strncmp! Don't want a buffer overflow!
        – Floam
        Nov 10 '17 at 18:36















      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.






      share|improve this answer



















      • 8




        the same in java,which may just compare with the address.
        – Telerik
        Sep 6 '14 at 16:29






      • 18




        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






      • 3




        It is safer to use strncmp! Don't want a buffer overflow!
        – Floam
        Nov 10 '17 at 18:36













      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.






      share|improve this answer














      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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




        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






      • 3




        It is safer to use strncmp! Don't want a buffer overflow!
        – Floam
        Nov 10 '17 at 18:36














      • 8




        the same in java,which may just compare with the address.
        – Telerik
        Sep 6 '14 at 16:29






      • 18




        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






      • 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












      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.






      share|improve this answer























      • 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 of fgets should be a char*. But stdin is a FILE*
        – Cool Guy
        Feb 11 '15 at 11:08















      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.






      share|improve this answer























      • 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 of fgets should be a char*. But stdin is a FILE*
        – Cool Guy
        Feb 11 '15 at 11:08













      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.






      share|improve this answer














      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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 of fgets should be a char*. But stdin is a FILE*
        – 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






      • 7




        @nmagerko yeah I understand. It's always important to realise that though.
        – AusCBloke
        Nov 4 '11 at 2:32






      • 2




        The first argument of fgets should be a char*. But stdin is a FILE*
        – 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










      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;
      }





      share|improve this answer



















      • 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. 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




        I would use || instead of &&.
        – lukasrozs
        Oct 6 '17 at 14:28















      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;
      }





      share|improve this answer



















      • 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. 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




        I would use || instead of &&.
        – lukasrozs
        Oct 6 '17 at 14:28













      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;
      }





      share|improve this answer














      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;
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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. 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




        I would use || instead of &&.
        – lukasrozs
        Oct 6 '17 at 14:28














      • 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. 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




        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










      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.






      share|improve this answer



























        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.






        share|improve this answer

























          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 23 '17 at 11:47









          Community

          11




          11










          answered Feb 13 '16 at 7:14









          fortunate_man

          3,73993355




          3,73993355






















              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");
              }





              share|improve this answer























              • Damn, you really need some spaces
                – jv110
                Aug 22 '17 at 20:17















              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");
              }





              share|improve this answer























              • Damn, you really need some spaces
                – jv110
                Aug 22 '17 at 20:17













              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");
              }





              share|improve this answer














              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");
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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


















              • 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










              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.






              share|improve this answer

















              • 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















              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.






              share|improve this answer

















              • 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













              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.






              share|improve this answer












              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              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














              • 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










              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.






              share|improve this answer

























                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.






                share|improve this answer























                  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.






                  share|improve this answer












                      #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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 23 at 7:26









                  Rupani T D

                  114




                  114

















                      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?



                      Popular posts from this blog

                      The Sandy Post

                      Danny Elfman

                      Pages that link to "Head v. Amoskeag Manufacturing Co."