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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the difference between strcpy() and memcpy() function?

603


Can you subtract pointers from each other? Why would you?

570


How can I call a function with an argument list built up at run time?

651


Are pointers integers in c?

619


What is FIFO?

686






The number of measuring units from an arbitarary starting point in a record,area,or control block to some other point a) recording pointer b) offset c) branching d) none

723


What is a method in c?

633


Write a program to find factorial of a number using recursive function.

660


write a program to rearrange the array such way that all even elements should come first and next come odd

1770


Is c is a high level language?

630


What is the best way to comment out a section of code that contains comments?

792


An expression to whose value an operater is applied a) operand b) variable c) constant d) all of the above

670


how many types of operators are include in c language a) 4 b) 6 c) 8 d) 12

662


What are the uses of null pointers?

605


What are the benefits of c language?

657