Write a program to compare two strings without using the
strcmp() function

Answers were Sorted based on User's Feedback



Write a program to compare two strings without using the strcmp() function..

Answer / sumant maurya

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10];
char b[10];
int flag=0;
clrscr();
puts("enter the first string\n");
gets(a);
puts("enter the second string\n");
gets(b);
for(int i=0;i<=strlen(a);i++)
{
if(a[i]==b[i])
{
flag=1;
}
}
if(flag==1)
{
puts("matches");
}
else
{
puts("not matches");
}
getch();
}

Is This Answer Correct ?    6 Yes 8 No

Write a program to compare two strings without using the strcmp() function..

Answer / belsia

void main()
{
char a[10],b[10];
int i=0;
scanf("%s%s",a,b);
if(strlen(a)!=strlen(b))
printf("they are different strings");
else
{
while(a[i]!='\0')
{
if(a[i]==b[i])
i++;
else
{
printf("They are different strings");
exit(0);
}
}
printf("Both are same");
}
getch();
}

Is This Answer Correct ?    10 Yes 13 No

Write a program to compare two strings without using the strcmp() function..

Answer / kedir

1 yes
2 yes

Is This Answer Correct ?    29 Yes 33 No

Write a program to compare two strings without using the strcmp() function..

Answer / ria varughese

#include <stdio.h>
#include <string.h>

void stringcmp(char s1[], char s2[]);

int main()
{

char str1[10],str2[10];

printf("\nEnter first String:");
scanf("%s",str1);

printf("\nEnter second String:");
scanf("%s",str2);

stringcmp(str1,str2);

return 0;
}

void stringcmp(char *s1, char *s2)
{
int i,j;

for(i=0;s1[i]!='\0';i++)
{
for(j=0;s2[j]!='\0';j++)
{
if(s1[i] == s2[j])
continue;
}
}

if (i==j)
{
printf("String s1:%s and s2:%s are EQUAL\n",s1,s2);
}
else
printf("String s1:%s and s2:%s are NOT EQUAL\n",s1,s2);

}

Is This Answer Correct ?    84 Yes 89 No

Write a program to compare two strings without using the strcmp() function..

Answer / yathish m yadav

#include<conio.h>
int strcmp(char *,char *);

char a[10],b[10];
void main()
{
int c;
printf("enter the first string\n");
scanf("%s",a);
printf("enter the second string\n");
scanf("%s",b);
c=strcmp(a,b);
switch(c)
{
case 1: printf("first string is larger then second");
break;
case 2: printf("second is greater than first");
break;
case 3: printf("not equal");
break;
case 4: printf("the strings are equal");
break;
}
getch();
}

int strcmp(char *p,char *q)
{
int m,n;
m=strlen(p);
n=strlen(q);
if(m>n){
return (1);}
else if(n>m){
return (2);}
for(i=0;i<m;i++)
{
if(p[i]!=q[i])
return(3);
}
return(4);
}

Is This Answer Correct ?    4 Yes 10 No

Write a program to compare two strings without using the strcmp() function..

Answer / akash aggarwal

Guys What the hell????

strcmp tells you whether strings are equal or not, if not which is greater then other.....

So first compare their length...

example let
str1="abc";
str2="abcde";

now compare their lengths, if len1 >len2 means str1 is greater else
if len2>len1 then str2 is greater

else the above compare is required...........

Is This Answer Correct ?    4 Yes 12 No

Write a program to compare two strings without using the strcmp() function..

Answer / sujith

I have been seeing lot of answers posted on top of mine.
here is another highly optimized version.

int str_cmp (const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(unsigned char *)s1 - *(unsigned char *)--s2);
}
before marking it as not an answer, I urge you to try it once!
Trust me, it works.

Is This Answer Correct ?    6 Yes 15 No

Write a program to compare two strings without using the strcmp() function..

Answer / vikas patel

/*A program to compare of string */
#include<stdio.h>
#include<conio.h>
str_len1(char *s);
str_len2(char *p);
void main()
{
char arr1[20];
char arr2[20];
int len1,len2;
clrscr();
printf("\nEnter the frist string -> ");
scanf("%s",arr1);
printf("\nEnter the second string -> ");
scanf("%s",arr2);
len1 = str_len1(arr1);
len2 = str_len2(arr2);
if(len1==len2)
{
printf("Both string is equal");
}
else
{
printf("Both string is not equal");
}
getch();
}
str_len1(char *s)
{
int length = 0;
while(*s != '\0')
{
length++;
s++;
}
return(length);
}
str_len2(char *p)
{
int a = 0;
while(*p != '\0')
{
a++;
p++;
}
return(a);
}

Is This Answer Correct ?    3 Yes 13 No

Write a program to compare two strings without using the strcmp() function..

Answer / shaiju . a

int str_cmp( const char *str1 , const char *str2)
{


while(*str1 != '\0')
{
if( *str1 == *str2)
{
str1++;
str2++;
}
else
break;
}


return *str1 - *str2;
}

Is This Answer Correct ?    64 Yes 78 No

Write a program to compare two strings without using the strcmp() function..

Answer / sandeep a

// Optimize the above soln...
#include<stdio.h>
int str_cmp(const char *s1, const char *s2)
{
unsigned int i = 0, diff;
while(s1[i]!= '\0' || s2[i] != '\0'){
diff = s1[i] - s2[i];
if(!diff)i++;
else break;
}
return diff;
}
int main(int argc, char *argv[1])
{
printf("chuma %d ", str_cmp("abcd","abcde"));
return 0;
}

Is This Answer Correct ?    35 Yes 49 No

Post New Answer

More C Interview Questions

Write a program to exchange two variaables without temp

9 Answers   Geometric Software,


What is meant by operator precedence?

0 Answers  


Tell me is null always defined as 0(zero)?

0 Answers  


Is Exception handling possible in c language?

0 Answers   Wipro,


What is the size of enum in c?

0 Answers  






What is the purpose of 'register' keyword?

0 Answers  


What is getch() function?

0 Answers  


int main() { int i=1; switch(i) { case '1': printf("hello"); break; case 1: printf("Hi"); break; case 49: printf("Good Morning"); break; } return 0; }

3 Answers  


How to write a program for swapping two strings without using 3rd variable and without using string functions.

7 Answers   iGate, Infotech,


How can you return multiple values from a function?

0 Answers  


Why static is used in c?

0 Answers  


I have a function which accepts, and is supposed to initialize,a pointer, but the pointer in the caller remains unchanged.

1 Answers  


Categories