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

Explain what are compound statements?

0 Answers  


int a[3][5]={ {1,2,3,4,5],{2,3,4,5,6},{10,11,12,13,14}}; int *p=&a; printf(“%d”,*(*(x+1)+3));

2 Answers   Wipro,


Program to find largest of three numbers without using comparsion operator?

3 Answers  


write a program that will accept two integers and will implement division without using the division operator if the second value is an odd number and will implement multiplication without using multiplication operator if the second value is an even number.

1 Answers  


why do we use # in c-language?

1 Answers  


What is unsigned int in c?

0 Answers  


When should I declare a function?

0 Answers  


post new interiew question and aptitude test papers

1 Answers  


What is atoi and atof in c?

0 Answers  


how to display 2-D array elements in spiral

2 Answers  


What is omp_num_threads?

0 Answers  


why java is called as a purely oops language.

3 Answers   TVS,


Categories