Monday, 23 November 2015

C++ Program To Compare Two String Without Using Library Function

Problem :-  Write A C++ Program To Compare Two String Are Equal Or Not Without Using Library Function.

Logic :- In this problem you can use two method one is using library function second is without using library function 


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;
  }
}

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

Solution :-

#include<bits/stdc++.h>
using namespace std;
int main()
{
  //By-Ghanendra Yadav 
  char str1[20],str2[20],i=0,j=0,flag=0;

  cout<<"Enter 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;
  }
  }
  }
  if(flag==0)
  {
  cout<<"Strings Are Not Equal\n";
  }
  else
  {
  cout<<"Strings Are Equal\n";
  }
return 0;
}

Output:-


C++ Program To Compare Two String Without Using Library Function

No comments:
Write comments

Recommended Posts × +