Write a program to compare two strings without using the
strcmp() function
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
/*what is the output for the code*/ void main() { int r; r=printf("naveen"); r=printf(); printf("%d",r); getch(); }
Hai why 'c' is the middle language
What is dangling pointer in c?
Write down the program to sort the array.
What is volatile keyword in c?
int main() { int x = (2,3,4); int y = 9,10,11; printf("%d %d",x,y); } what would be the output?
Give a fast way to multiply a number by 7
15 Answers Accenture, Aricent, Microsoft,
write a c program to print a given number as odd or even without using loop statements,(no if ,while etc)
What are the types of operators in c?
What is the difference between getch() and getche()?
Write a program that accepts a string where multiple spaces are given in between the words. Print the string ignoring the multiple spaces. Example: Input: “ We.....Are....Student “ Note: one .=1 Space Output: "We Are Student"
program to find which character is occured more times in a string and how many times it has occured? for example in the sentence "i love india" the output should be i & 3.