Monday, 23 November 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 the both string is not equal then program print the message "Both strings are not equal" and if the size of the both sting is equal then program compare the both string character by character and return if the both string is equal or not according to the string analysis. Below is the 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.

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

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:-


Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

2 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