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 / raghu ram. n

#include<stdio.h>
void main()
{ char string1[50], string2[50];
int i=0;
printf("Enter First String :");
gets(string1);
printf("Enter Second String :");
gets(string2);
while(string1[i]!=0&&string2[i]!=0&&srting1[i]==string2[i])
i++;
if(string1[i]=='\0'&&string2[i]=='\0')
printf("Two strings are equal");
else
printf("Two string are not equal");
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / sanskriti jain

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a[50],b[50];
int c,i=0;
cout<<"\n Enter the first string:\n";
gets(a);
cout<<"\n Enter the second string:\n";
gets(b);
while (a[i]!='\0'||b[i]!='\0')
{
c=(a[i]-b[i]);
if(c!=0)
break;
i++;
}
if(c>0)
cout<<"a comes after b\n"<<a<<"\n"<<b;
else
{
if(c<0)
cout<<"b comes after a\n"<<b<<"\n"<<a;
else
cout<<"Both the string are same\n"<<a;
}
getch();
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / bharat prajapati

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

main()

{

char s1[10],s2[10];
int i,j;
clrscr();

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

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

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);
getch();

}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / madiha

“PROGRAM THAT COPMPARE TWO STRINGS”

#include <iostream>
using namespace std;

int mycomp(char str1[],char str2[])
{
int i=0;
while(1)
{
if(str1[i]>str2[i])
return 1;
if(str1[i]<str2[i])
return -1;
else if(str1[i]=='\0'||str2[i]=='\0')
return 0;
i++;
}
}
int main()
{
cout<<mycomp("madiho","madiha");
return 0;
}

OUTPUT
1

Is This Answer Correct ?    0 Yes 0 No

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

Answer / navy

The previous post is for finding whether string is
pallindrome or not

Is This Answer Correct ?    1 Yes 1 No

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

Answer / muku

all are wrong answer

Is This Answer Correct ?    0 Yes 0 No

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

Answer / nomi mayana

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
char a[25],b[25];
int cnt=1,i=1;
cout<<"\n ENTER THE STRING 1 : ";
cin>>a;
cout<<"\n ENTER THE STRING 2 : ";
cin>>b;

while(a[i]!='\0' && b[i]!='\0')
{
if(a[i]==b[i])
{
cnt++;
}
else
{
cout<<"\n\n THE TWO STRINGS ARE NOT EQUAL";
}
i++;
}

if(i==cnt)
{
int n;

cout<<"\n\t\t~~~~~~~~~~COMPARISON RESULT~~~~~~~~~~~";
cout<<"\n THE GIVEN TWO STRINGS ARE SAME ";
}

//getch();
return 0;
}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / aiattack

#include <stdio.h>
#include <string.h>
int main()
{
char a[10];
char b[10];
int check = 0;
gets(a);
gets(b);
if (strlen(a)!=strlen(b)) {
printf("Strings don't match!");
} else {
for(int i=0, j=0; a[i] != '' || b[i] != ''; i++, j++) {
if(a[i] == b[i]) {
check = 0;
} else {
check ++;
break;
}
}
if (check == 0) printf("Strings match!");
else printf("Strings don't match");
}
return 0;
}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / fionaa

int compare(char str2[], char str1[]) {
int flag = -1;
int i=0;
while(str1[i]!='\0' && str2[i]!='\0'){
if((str1[i]==str2[i])) {flag = 0;}
else if (str1[i]>str2[i]) {
flag=-1;
break;
}else if(str1[i]<str2[i]){
flag = 1;
break;
}
i++;
}

if(strlen(str1)==strlen(str2) && flag==0 ){
flag = 0;
}
else if(strlen(str1)>strlen(str2) && flag==0 ){
flag = 1;
}
else {flag = -1;}
return flag;
}

Is This Answer Correct ?    0 Yes 1 No

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

Answer / abhishek

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char a[100],b[100];
int i,m;
cout<<"enter first string";
gets(a);
cout<<"enter second string";
gets(b);
for(i=0;a[i]!='\0'&&b[i]!='\0';i++)
{
if(a[i]!=b[i])
{
c=1;
}
}
if(c==1)
{cout<<"the strings do not match!";
}
else
{
cout<<"the strings match!";
}
getch();
}

Is This Answer Correct ?    7 Yes 9 No

Post New Answer

More C Interview Questions

what is the definition of storage classes?

3 Answers   Wipro,


Explain what is wrong with this program statement?

0 Answers  


write a program to search for an element in a given array. If the array was found then display its position otherwise display appropriate message in c language

18 Answers   IT Park, TCS,


What is a substring in c?

0 Answers  


What is the purpose of the statement: strcat (S2, S1)?

0 Answers  






we all know about the function overloading concept used in C++ and we all learnt abt that.... but that concept is already came in C in a very smaller propotion ... my question is IN WHICH CONCEPT THERE IS A USE OF FUNCTION OVERLOADING IS USED in C language?????????????

4 Answers   Google,


How do I copy files?

0 Answers  


What are the basic data types associated with c?

0 Answers  


Is there any demerits of using pointer?

0 Answers  


Write a program that receives as input a number omaadel-n-print, four digits.

0 Answers  


write a program to print data of 5 five students with structures?

0 Answers  


How can I send mail from within a c program?

0 Answers  


Categories