Write a program to compare two strings without using the
strcmp() function
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
Answer / navy
The previous post is for finding whether string is
pallindrome or not
Is This Answer Correct ? | 1 Yes | 1 No |
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 |