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

Why is structure padding done in c?

0 Answers  


How do you redirect a standard stream?

0 Answers  


What is a nested formula?

0 Answers  


WHY DO WE USE A TERMINATOR IN C LANGUAGE?

2 Answers  


main() { int x=5; printf("%d %d %d\n",x,x<<2,x>>2); }

11 Answers   CISOC, CitiGroup, College School Exams Tests,






7-Given an index k, return the kth row of the Pascal's triangle. For example, when k = 3, the row is [1,3,3,1]. For reference look at the following standard pascal’s triangle.

0 Answers  


pascal triangle program

2 Answers  


Write the program with at least two functions to solve the following problem. The members of the board of a small university are considering voting for a pay increase for their 5 faculty members. They are considering a pay increase of 8%. Write a program that will prompt for and accept the current salary for each of the faculty members, then calculate and display their individual pay increases. At the end of the program, print the total faculty payroll before and after the pay increase, and the total pay increase involved.

1 Answers  


Which is not valid in C? 1) class aClass{public:int x;} 2) /* A comment */ 3) char x=12;

7 Answers  


Bit swapping

2 Answers  


List out few of the applications that make use of Multilinked Structures?

1 Answers   Accenture,


What does malloc () calloc () realloc () free () do?

0 Answers  


Categories