How do I properly compare strings?












147














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
















147














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














147












147








147


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

106k17156260




106k17156260










asked Nov 4 '11 at 2:22









nmagerko

2,77962963




2,77962963








  • 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


















227














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






  • 19




    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



















30














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*
    – Spikatrix
    Feb 11 '15 at 11:08



















6














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



















5














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































    1














    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



















    0














    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



















    0














        #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









      227














      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






      • 19




        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
















      227














      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






      • 19




        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














      227












      227








      227






      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

      559k896651016




      559k896651016










      answered Nov 4 '11 at 2:24









      Mysticial

      380k40291300




      380k40291300








      • 8




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






      • 19




        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






      • 19




        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




      19




      19




      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













      30














      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*
        – Spikatrix
        Feb 11 '15 at 11:08
















      30














      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*
        – Spikatrix
        Feb 11 '15 at 11:08














      30












      30








      30






      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

      559k896651016




      559k896651016










      answered Nov 4 '11 at 2:29









      AusCBloke

      14.8k53443




      14.8k53443












      • 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*
        – Spikatrix
        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*
        – Spikatrix
        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*
      – Spikatrix
      Feb 11 '15 at 11:08




      The first argument of fgets should be a char*. But stdin is a FILE*
      – Spikatrix
      Feb 11 '15 at 11:08











      6














      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
















      6














      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














      6












      6








      6






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




      1,97021427










      answered Apr 6 '15 at 9:46









      mugetsu

      101110




      101110








      • 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











      5














      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




























        5














        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


























          5












          5








          5






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




          3,75093455























              1














              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
















              1














              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














              1












              1








              1






              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











              0














              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
















              0














              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














              0












              0








              0






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




              1,62711533








              • 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











              0














                  #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


























                0














                    #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
























                  0












                  0








                  0






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



                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings