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

tell me the full form of c?

2 Answers  


Write a C program to perform some of the operation which can be performed using Single linked list

1 Answers   Qualcomm,


Compare array data type to pointer data type

0 Answers  


What is a program flowchart and explain how does it help in writing a program?

0 Answers  


A marketing company wishes to construct a decision table to decide how to treat clients according to three characteristics: Gender, City Dweller, and age group: A (under 30), B (between 30 and 60), C (over 60). The company has four products (W, X, Y and Z) to test market. Product W will appeal to female city dwellers. Product X will appeal to young females. Product Y will appeal to Male middle aged shoppers who do not live in cities. Product Z will appeal to all but older females.

2 Answers  


List some of the static data structures in C?

0 Answers  


How are pointers declared in c?

0 Answers  


How do you do dynamic memory allocation in C applications?

0 Answers  


How to explain the final year project as a fresher please answer with sample project

0 Answers  


what is the difference between entry control and exit control statement?

12 Answers   Darbari Lal DAV Model School,


What is the difference between array and pointer?

0 Answers  


There are 3 baskets of fruits with worng lables,one basket has apple,another basket has orange,another has combination of apple and orange,what is the least way of interchange the lables.

15 Answers   Cisco, Google, MBT,


Categories