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 / kms

The following code compares the strings alphabetically:

For example:

1. "bite" is greater than "apple" even though length of bite
is less than apple. ( it is greater lexicographically)

2. "It is good" is lower than "It is great".

CODE -->

#include<stdio.h>
#include<conio.h>

void strings_compare(char [], char []);

void main()
{
char str1[50], str2[50];
clrscr();
flushall();
printf("String 1 : ");
gets(str1);
flushall();
printf("String 2 : ");
gets(str2);
strings_compare(str1,str2);
getch();
}

void strings_compare(char str1[], char str2[])
{
int i=0,j=0,flag1=0,flag2=0;
printf("\nString 1 : ");
puts(str1);
printf("\nString 2 : ");
puts(str2);
printf("\n");
while(str1[i] != '\0' || str2[j] != '\0')
{
if(str1[i] < str2[j])
{
flag1 = 1;
break;
}
if(str1[i] > str2[j])
{
flag2 = 1;
break;
}
else
{
i++;
j++;
}
}

if(flag1==1)
{
printf("\n\ns1 : %s is lower than s2 : %s",str1,str2);
}
else if(flag2 == 1)
{
printf("\n\ns1 : %s is greater than s2 : %s",str1,str2);
}
else
{
printf("\n\nBoth strings are equal...");
}
}

Is This Answer Correct ?    6 Yes 3 No

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

Answer / md.ershad.ezaz

int strcompare(char *,char *);
void main()
{
char s1[15],s2[15];
int cmp;
clrscr();
puts("Enter first string");
gets(s1);
puts("Enter second string");
gets(s2);
cmp=strcompare(s1,s2);
if(cmp==0)
puts("strings are equal");
else
puts("strings are not equal");
getch();
}
int strcompare(char *p1,char *p2)
{
while(*p1==*p2)
{
if(*p1=='\0')
return(0);
p1++;
p2++;
}
return(*p1-*p2);
}

Is This Answer Correct ?    4 Yes 1 No

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

Answer / raj

#include<stdio.h>
void main()
{
char name[80],name1[80];
int i;
printf("enter 1st string");
gets(name);
printf("enter 2st string");
gets(name1);
i=0;
while(name[i]==name1[i] && name1[i]!='\0')
i++;
if(name[i]==name1[i])
printf("two strings r equal\n");
else
printf("two strings r not equal\n");
}

Is This Answer Correct ?    4 Yes 1 No

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

Answer / shashi

#include<stdio.h>
#include<conio.h>

int 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);


if (stringcmp(str1,str2))
{
printf("String s1:%s and s2:%s are EQUAL\n",str1,str2);
}
else
printf("String s1:%s and s2:%s are NOT EQUAL\n",str1,str2);
getch();

return 0;
}

int stringcmp(char *s1, char *s2)
{
int flag=0;
char *count;
count=s1;
while(*count++)
{
flag=0;
if(*s1++==*s2++)
{
flag=1;
}
}

return flag;

}

Is This Answer Correct ?    4 Yes 1 No

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

Answer / abhi

#include<stdio.h>
#include<conio.h>

main()
{
char *s1;
char *s2;

int flag=0;
s1=(char *)malloc(10);
s2=(char *)malloc(10);
scanf("%s",s1);
scanf("%s",s2);


while((*s1!='\0')||(*s2!='\0'))
{
if(*s1!=*s2)
flag=1;
else
{
s1++;
s2++;
}
if(flag)
{
if(*s1<*s2)
flag=-1;
break;
}}

if(*s1=='\0'&&*s2!='\0')
flag=-1;
else if(*s1!='\0'&&*s2=='\0')
flag=1;
else
{}
printf("%d",flag);


getch();
}

Is This Answer Correct ?    4 Yes 1 No

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

Answer / navy

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20];
int i, j, len=0, flag=0;
printf("\nEnter any string: ");
gets(s1);
for (i=0; s1[i]!='\0'; i++)
len++;
i = 0;
j = len-1;
while (i < len)
{
if (s1[i] != s1[j])
{
flag = 1;
break;
}
i++;
j--;
}
if (flag == 0)
printf("\nString is palindrome");
else
printf("\nString is not palindrome");
getch();
}

Is This Answer Correct ?    4 Yes 1 No

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

Answer / sujith

#include<stdio.h>
int str_cmp(const char *s1, const char *s2)
{
unsigned int i = 0, diff;
while(*(s1+i) && *(s2+i))
{
diff = (*(s1+i)-*(s2+i));
if(!diff)i++;
else break;
}
return diff;
}
int main()
{
printf("chuma %d ", str_cmp("abcd","abcde"));
return 0;
}
U can use this as a prototype and enhance this. I havent
even tried compilng this.
Sujith

Is This Answer Correct ?    115 Yes 113 No

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

Answer / fionaa

corrected:

Returns an integral value indicating the relationship
between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character
that does not match has a greater value in str1 than in
str2; And a value less than zero indicates the opposite.

int compare(char str1[], char str2[]) {
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 if(strlen(str1)<strlen(str2) && flag==0 ){flag = -1;}
return flag;

}

Is This Answer Correct ?    4 Yes 2 No

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

Answer / anil sai krishna

#include<stdio.h>
main()
{
char a[15],b[15];
int i,l=0,m;
printf("enter the two strings");
scanf("%s%s",a,b);
for(i=0;a[i]=b[i];i++)
{
l++;
}
for(i=0;a[i]!='\0';i++)
{
m++;
}
if(l==m)
{
printf("strings are equal");
}
else
{
printf("not equal");
}
}

Is This Answer Correct ?    7 Yes 5 No

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

Answer / shashank kapoor

//PROGRAM TO COMPARE TWO STRINGS
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[10],ptr[10];
int 1,flag,a,b,x,y;
cout<<"Enter two strings:"<<endl;
cin>>str;
a=strlen(str);
cin>>ptr;
b=strlen(ptr);
for(i=0;i<(a>b?a:b);i++)
{
if(str[i]>ptr[i])
{
x=str[i]-ptr[i];
flag=1;
}
else if(str[i]<ptr[i])
{
y=str[i]-ptr[i];
flag=2;
}
}
if(flag==1)
cout<<"Str is greater than Ptr by: "<<x;
else if(flag==2)
cout<<"Str is smaller than Ptr by: "<,y;
else
cout<<"Str & Ptr are EQUAL";
getch();
}

Is This Answer Correct ?    3 Yes 1 No

Post New Answer

More C Interview Questions

Two's compliment of -5

4 Answers   Adobe,


Explain the difference between the local variable and global variable in c?

0 Answers  


Can the curly brackets { } be used to enclose a single line of code?

0 Answers  


Compare array data type to pointer data type

0 Answers  


What is a protocol in c?

0 Answers  


Why can’t constant values be used to define an array’s initial size?

0 Answers  


Explain what’s a signal? Explain what do I use signals for?

0 Answers  


When do you say that a digraph is acyclic A)if and only if its first search does not have back arcs B)a digraph is acyclic if and only if its first search does not have back vertices C)if and only if its first search does not have same dfnumber D)None of these

1 Answers   Accenture, IBM,


Hai sir, I had planned to write the NIC scientific engineer exam , plz post the sample question......

0 Answers  


which of the following is not a character constant a) 'thank you' b) 'enter values of p, n ,r' c) '23.56E-o3' d) all of the above

0 Answers  


What is the concatenation operator?

0 Answers  


. Consider the following program main() { int a[5]={1,3,6,7,0}; int *b; b=&a[2]; } The value of b[-1] is (A) 1 (B) 3 (C) -6 (D) none

9 Answers   Oracle,


Categories