Write a program to compare two strings without using the
strcmp() function
Answer Posted / 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 View All Answers
What is the difference between local variable and global variable in c?
Do variables need to be initialized?
Explain a file operation in C with an example.
How do you use a 'Local Block'?
Tell me what are bitwise shift operators?
why arguments can generally be passed to functions a) sending the values of the arguments b) sending the addresses of the arguments c) a & b d) none of the above
hi send me sample aptitude papers of cts?
What is bin sh c?
What is #include cctype?
hi, which software companys will take,if d candidate's % is jst 55%?
What are the valid places to have keyword “break”?
Can we compile a program without main() function?
Write a program to reverse a given number in c language?
write a program that reads lines(using getline), converts each line to an integer using atoi, and computes the average of all the numbers read. also compute the standard deviation.
write a progrmm in c language take user interface generate table using for loop?