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


Please Help Members By Posting Answers For Below Questions

What does dm mean sexually?

799


Which function in C can be used to append a string to another string?

637


Can you please compare array with pointer?

605


can anyone please tell about the nested interrupts?

1668


Why is c so powerful?

667






What is the basic structure of c?

547


What are the functions to open and close the file in c language?

581


Is it fine to write void main () or main () in c?

535


Write a program to swap two numbers without using a temporary variable?

599


What is dynamic variable in c?

551


What Is The Difference Between Null And Void Pointer?

629


What is the function of this pointer?

657


How to write a multi-statement macro?

610


In this problem you are to write a program that will cut some number of prime numbers from the list of prime numbers between 1 and N.Your program will read in a number N; determine the list of prime numbers between 1 and N; and print the C*2 prime numbers from the center of the list if there are an even number of prime numbers or (C*2)-1 prime numbers from the center of the list if there are an odd number of prime numbers in the list.

1368


How can my program discover the complete pathname to the executable from which it was invoked?

650