Q:

C Program to Compare Two Strings Without Using strcmp Function

0
Logic:-
 In this problem, you can use two methods one is using library function secondly is without using a library function, but we use here without using a strcmp() function in this we first calculate the size of a both string and if the size of both strings is not equal then program print the message "Both strings are not equal" and if the size of both stings is equal then program compare both string character by character and return if both string is equal or not according to the string analysis. Below is both method for comparing a string using library function or without using library functions.

Using Library Function
strcmp(string1 ,string2)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<bits/stdc++.h>
using namespace std;
int main()
{

   
    cout<<"=====================================";
    cout<<"\nVisit - www.nerdutella.com";
    cout<<"\n=====================================";
    
   char str1[20],str2[20],i=0,j=0,flag=0;

   cout<<"\n\nEnter First String : \n";
   gets(str1);
  
   cout<<"Enter Second String : \n";
   gets(str2);
   
    while(str1[i]!='\0')
    {
     i++;
    }
   
    while(str2[j]!='\0')
    {
     j++;
    }
  
   if(i!=j)
   {
   flag=0;
   }
  
   else
   {
   for(i=0,j=0;str1[i]!='\0',str2[j]!='\0';i++,j++)
   {
   if(str1[i]==str2[j])
   {
   flag=1;
   }
   else
   {
   flag=0;

   }
   }
   }
  
   if(flag==0)
   {
   cout<<"Both Strings Are Not Equal\n";
   }
   else
   {
   cout<<"Both Strings Are Equal\n";
   }
  return 0;
}

 

Output:

=====================================

Visit - www.nerdutella.com

=====================================

Enter First String : 

example

Enter Second String : 

programming

Both Strings Are Not Equal

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to Copy One String into Another String... >>
<< C++ Program to Find The Length of a String Without...