Compare two strings in C using strcmp
Hey guys once again welcome to the Decoders blog, I hope you all are doing well and today I'm present with another logic but this time we will compare two different strings in C using the strcmp functionality from the <string.h> library.....
#include<stdio.h>
#include<string.h>
int main()
{
//Comparing the two strings of a given input
int n,i;
char str1[n][30],str2[n][30];
printf("Enter the number of words for the string\n");
scanf("%d",&n);
printf("Enter the inputs for the first array\n")
for(i=0;i<n;i++)
{
scanf("%s",&str1[i]);
}
printf("Enter the inputs for the second array\n");
for(i=0;i<n;i++)
{
scanf("%s",&str2[i]);
}
//Displaying the final array
printf("\nThe first array is \n");
for(i=0;i<n;i++)
{
printf("%s ",str1[i]);
}
printf("\nThe second array is \n");
for(i=0;i<n;i++)
{
printf("%s ",str2[i]);
}
//Comparing the two strings
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(strcmp(str1[i],str2[j])==0)
{
printf("\nThe %dth index of the first strings is equal
to the %dth index of the second array\n",i,j);
}
}
}
}
Comments
Post a Comment