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

What are the different types of C instructions?

0 Answers   InterGraph,


an algorithem for the implementation of circular doubly linked list

1 Answers  


difference between string and array?

6 Answers  


Explain what is meant by 'bit masking'?

0 Answers  


how can f be used for both float and double arguments in printf? Are not they different types?

0 Answers  






Explain c preprocessor?

0 Answers  


what is out put of the following code? #include class Base { Base() { cout<<"constructor base"; } ~Base() { cout<<"destructor base"; } } class Derived:public Base { Derived() { cout<<"constructor derived"; } ~Derived() { cout<<"destructor derived"; } } void main() { Base *var=new Derived(); delete var; }

3 Answers   Honeywell,


which is the best site or book for learning C...and i need the content for C..how to get the good programming skills....? can plz suggest me....

2 Answers  


What is clrscr ()?

0 Answers  


At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with these names could be kept in that shelf as in this example: bottom of shelf ---> [AAAJKRDFDEWAAYFYYKK]-----Top of shelf. All these packets are to be loaded on cars. The cars are lined in order, so that the packet could be loaded on them. The cars are also named [A, B, C, D, E,………….]. Each Car will load the packet with the same alphabet. So, for example, car ‘A’ will load all the packets with name ‘A’. Each particular car will come at the loading point only once. The cars will come at the loading point in alphabetical order. So, car ‘B’ will come and take all the packets with name ‘B’ from the shelf, then car ‘C’ will come. No matter how deep in the shelf any packet ‘B’ is, all of the ‘B’ packets will be displaced before the ‘C’ car arrives. For that purpose, some additional shelves are provided. The packets which are after the packet B, are kept in those shelves. Any one of these shelves contains only packets, having the same name. For example, if any particular shelf is used and if a packet with name X is in it, then only the packets having names X will be kept in it. That shelf will look like [XXXXXXX]. If any shelf is used once, then it could be used again only if it is vacant. Packets from the initial shelf could be unloaded from top only. Write a program that finds the minimum total number of shelves, including the initial one required for this loading process.

0 Answers   Infosys,


Is main() is used in the program,,see below example? void main() { int i; for(i=0;i<10;i++) main(); } Then what is the output of the program?

6 Answers  


What is variable declaration and definition in c?

0 Answers  


Categories