Write a program to compare two strings without using the
strcmp() function
Answers were Sorted based on User's Feedback
Answer / waqar nawaz
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int c=0;
char a[10];
char b[10];
gets(a);
gets(b);
for(int i=0,j=0;b[i]!='\0'||a[j]!='\0';i++,j++)
{
if(a[i]!=b[j])
{
c++;
}
}
if(c==0)
cout<<"string match";
else
cout<<"string does not match";
getch();
}
Is This Answer Correct ? | 104 Yes | 51 No |
Answer / premkumar
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int c=0;
char a[10];
char b[10];
gets(a);
gets(b);
for(int i=0,j=0;b[i]!='\0'||a[j]!='\0';i++,j++)
{
if(a[i]!=b[j])
{
c++;
}
}
if(c==0)
cout<<"string match";
else
cout<<"string does not match";
getch();
}
Is This Answer Correct ? | 69 Yes | 24 No |
Answer / vijay.benzamin
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int c=0;
char a[10];
char b[10];
gets(a);
gets(b);
for(int i=0,j=0;b[i]!='\0'||a[j]!='\0';i++,j++)
{
if(a[i]!=b[j])
{
c++;
}
}
if(c==0)
cout<<"string match";
else
cout<<"string does not match";
getch();
}
Is This Answer Correct ? | 54 Yes | 31 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 ? | 27 Yes | 16 No |
Answer / sreevalli kamineni
main()
{
char a[50],b[50];
int i=0,flag;
puts("enter 1st string");
gets(a);
puts("enter 2nd string");
gets(b);
if(strlen(a)!=strlen(b))
puts("not identical");
else
{
while(a[i]!='\0')
{
if(a[i]==b[i])
{
i++;
flag=1;
}
else
{
puts("not identical");
}
}
if(flag==1) puts("identical");
}
Is This Answer Correct ? | 13 Yes | 3 No |
Answer / b.l.paliwal
ALL PERSON ARE STUPIDS THEY DONT KNOW HERE WAT THE FUNCTION
STRCMP DO IT COMPARE BOTH STRING AND RETURN AN INTEGER
VALUE BUT ALL STUPID ARE COMPARING THE LENGTH OF STRING NOT
CHARACTER
WHAT WILL IF I WRITE RISHI
RISHI
HAAA HAA IT WILL RETURN 0
OR IF I WRITE RUCHI
SUCHI
IT WILL RETURN -1
STUPID THINK BEFORE POST UR PROGRAMME
Is This Answer Correct ? | 16 Yes | 7 No |
Answer / allen
#include<stdio.h>
#include<conio.h>
void stringcmp(char s1[], char s2[]);
void main()
{
char str1[10],str2[10];
printf("\nEnter first String:");
scanf("%s",str1);
printf("\nEnter second String:");
scanf("%s",str2);
stringcmp(str1,str2);
}
void stringcmp(char *s1, char *s2)
{
int i,j,c=0;
for(i=0,j=0;s1[i]!='\0'||s2[j]!='\0';i++,j++)
{
if(s1[i]!=s2[j])
{
c++;
}
}
if(c==0)
printf("\nstring match");
else
printf("\nstring does not match");
}
Is This Answer Correct ? | 12 Yes | 4 No |
Answer / bhagwati lal paliwal(b.l.paliw
ALL PERSON ARE STUPIDS THEY DONT KNOW HERE WAT THE FUNCTION
STRCMP DO IT COMPARE BOTH STRING AND RETURN AN INTEGER
VALUE BUT ALL STUPID ARE COMPARING THE LENGTH OF STRING NOT
CHARACTER
Now the right answer by me follow me............
and tell me if this code work properly on
bl_paliwal77@yahoo.com
#include<stdio.h>
#include<string.h>
int cmpstr(char s1[10], char s2[10]);
int main() {
char arr1[10] = "Nodalo";
char arr2[10] = "nodalo";
printf(" %d", cmpstr(arr1, arr2));
return 0;
}
int cmpstr(char s1[10], char s2[10])
{
int i = strlen(s1);
int k = strlen(s2);
int bigger;
if (i < k) {
bigger = k;
}
else if (i > k) {
bigger = i;
}
else {
bigger = i;
}
for (i = 0; i < bigger; i++) {
if (s1[i] == s2[i])
{
//do nothing if value of both string are equal
}
else {
return (s1[i] - s2[i]);
}
}
return (0);
}
Is This Answer Correct ? | 9 Yes | 5 No |
Answer / dinesh
#include<stdio.h>
#include<conio.h>
void main()
{
char name1[20],name[20];
int i,j,c=0;
clrscr();
printf("enter two strings");
scanf("%s%s",name1,name);
for(i=0,j=0;name1[i]!='\0'||name[j]!='\0';i++,j++)
{
if(name1[i]!=name[j])
{
c++;
}
}
if(c==0)
printf("equal strings");
else
printf("not equal strings");
}
Is This Answer Correct ? | 10 Yes | 6 No |
Answer / raghu
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100];
int i,c;
clrscr();
printf("enter first string");
scanf("%s",&a);
printf("enter second string");
scanf("%s",&b);
for(i=0;a[i]!='\0'&&b[i]!='\0';i++)
{
if(a[i]!=b[i])
{
c=1;
}
}
if(c==1)
{
printf("the strings do not match!");
}
else
{
printf("the strings match!");
}
getch();
}
Is This Answer Correct ? | 8 Yes | 4 No |
what is the significance of static storage class specifier?
What is the use of the function in c?
What is character set?
Find the middle node in the linked list?? (Note:Do not use for loop, count and count/2)
how to introdu5ce my self in serco
How to avoid structure padding in C?
What is const keyword in c?
which of the following is allowed in a "C" arithematic instruction a) [] b) {} c) () d) none of the above
How many ways are there to swap two numbers without using temporary variable? Give the each logic.
Write a main() program that calls this function at least 10 times. Try implementing this function in two different ways. First, use an external variable to store the count. Second, use a local variable. Which is more appropriate?
What are dangling pointers? How are dangling pointers different from memory leaks?
Why doesn't C have nested functions?