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 use of #define preprocessor in c?
What is the function of volatile in c language?
How to delete a node from linked list w/o using collectons?
What is #line used for?
what is the role you expect in software industry?
Why enum is used in c?
please send me the code for multiplying sparse matrix using c
How is pointer initialized in c?
Explain what is the purpose of "extern" keyword in a function declaration?
Explain what are binary trees?
What does *p++ do?
Write program to remove duplicate in an array?
Why is extern used in c?
What is build process in c?
What tq means in chat?