C

How To Compare Two Strings in C Using For Loop Without strcmp

In this tutorial, we are going to see how to compare two strings in C using for loop without strcmp. The following program uses For loop to iterate over each character in the string entered by the user, and compares each character individually.
 

How To Compare Two Strings in C Using For Loop Without strcmp
#include <stdio.h>

int main()
{
  char str1[100], str2[100];
  int i;
   
  printf("Enter the first string: ");
  gets(str1);

  printf("Enter the second string: ");
  gets(str2);
    
  for(i = 0; str1[i] == str2[i] && str1[i] == '\0'; i++);
         
  if(str1[i] > str2[i])
  {
    printf(" Both string are not equal.\n");
  }
  else if(str1[i] < str2[i])
  {
    printf(" Both string are not equal.\n");
  }
  else
  {
    printf(" Both string are equal.\n");
  }
    
  return 0;
}

Output:

Enter the first string: StackHowTo
Enter the second string: google
Both string are not equal.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *