C/C++ Program to program to compare two strings without using function strcmp, C/C++ Program to program to find whether first string is greater, smaller or equal to second string
Program
//program to compare two strings without using function strcmp
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[100], str2[100],m,n,i;
clrscr();
printf("Enter First String\n");
scanf("%s",str1);
printf("\nEnter Second String\n");
scanf("%s",str2);
m=strlen(str1);
n=strlen(str2);
if(m>n)
{
printf("\nString 1 is greater\n");
}
else if(m<n)
{
printf("\nstring 2 is greater\n");
}
else if(m==n)
{
for(i=0;i<m;i++)
if(str1[i]>str2[i])
{
printf("\nString 1 is greater\n");
break;
}
else if(str1[i]<str2[i])
{
printf("\nString 2 is greater\n");
break;
}
}
if(i==m)
printf("\nStrings are Equal\n");
getch();
}
Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h"
2.) for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
No comments:
Post a Comment