11/23/2015

C Program to Compare Two Strings Without Using strcmp Function

Problem:- Write A C++ Program To Compare Two String Are Equal Or Not Without Using Library Function or c++ program to compare two strings without using strcmp or C Program to Compare Two Strings without using strcmp function or C Program to Compare Two Strings Without Using strcmp or C program to compare two strings or How to compare two strings in c without using strcmp or Program to Compare Two Strings without using strcmp() in C++ or Write a program to compare two strings without using the strcmp() function or compare two strings without using strcmp in C++

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.


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)


Without Using Library Function

for(i=0,j=0;str1[i]!='\0',str2[j]!='\0';i++,j++)
{
if(str1[i]==str2[j])
{
flag=1;
}

else
{
flag=0;
}
}


This is a condition is used when the size of both strings is equal and we have to compare both the string character by character.


Sponsor: Install and access your important work applications and software no matter where you from any device ( PC / Android / iOS ) with a cloud desktop from www.CloudDesktopOnline.com. For cloud-related business software such as SharePoint, Office365, try Apps4Rent.


Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page.

Try Yourself - C++ Program To Check String Is Palindrome Or Not

Solution:-



#include<bits/stdc++.h>
using namespace std;
int main()
{
/*Visit - www.programmingwithbasics.com*/
   
    cout<<"=====================================";
    cout<<"\nVisit - www.programmingwithbasics.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:-


3 comments:

  1. for(i=0,j=0;str1[i]!='\0',str2[j]!='\0';i++,j++)
    {
    if(str1[i]==str2[j])

    flag=1;
    else
    flag=0 //There should be a else part...otherwise it fails to compare between hello &
    //helli
    }

    ReplyDelete
    Replies
    1. Thank you Very Much Nick Moni for notifying Errors. Now Program Is Updated


      Keep Sharing and Keep visiting

      Delete
  2. For instance :
    Abcde

    abcde
    -> 1 ( following your code )
    In fact, it was 0.

    ReplyDelete