strcmp () function in c progrom
Program to Compare Two Strings using strcmp( ) in C
The strcmp() compares two strings character by character. If the first character of two strings are equal, next character of two strings are compared. This continues until the corresponding characters of two strings are different or a null character '\0' is reached.
It is defined in string.h header file.
Example :
Input :
fist input atikur
2nd input arikur
output : sting are equal
program code :
#include<stdio.h>
#include<string.h>
int main()
{
char sr[100];
char ss[100];
printf("Enter the fist valu : ");
scanf("%s",&sr);
printf("Enter the 2nd valu : ");
scanf("%d",&ss);
int bb=strcmp(sr,ss);
if(bb==1)
printf("string are equal ");
else
printf(" string are not equal ");
return 0;
}
OUTPUT :
C program to compare two strings without using string function(strcmp)
This C program is to compare two strings without using string function(strcmp).For example, str1=”code” and str2=”code” then on comparing we find that the two strings are equal.
Logic
We first check if both their lengths are equal.If strings are not equal, we need not go further with computation, thus increasing the efficiency of the program.If they are equal then we compare each string character by character.
program code :
#include<stdio.h>
int main()
{
char ss[]="atikur";
char aa[]="atikur";
int i,j,b=1;
for(i=0;ss[i]!='\0';i++)
{
for(j=0;aa[j]!='\0';j++){
if(ss[i]==aa[j])
b=0;
else
b=1;
}
}
if(b==0)
printf("smae");
else
printf("not same ");
return 0;
}
or program code :
#include<stdio.h>
int main()
{
char ss[]="atikur";
char aa[]="atikur";
int i,j,b=1;
for(j=0; aa[j]!='\0'; j++)
{
if(ss[j]==aa[j])
b=0;
else
b=1;
}
if(b==0)
printf("smae");
else
printf("not same ");
return 0;
}
OUTPUT : same

No comments