program to check that string entered are anagram or not in cpp

program to check that string entered are anagram or not in cpp:

solution:

#include <iostream>
#include<string>
using namespace std;
int main() 
{
  char a[100],b[100];
  int flag=0;
  cout<<"enter ist string:";
  cin>>a;
  cout<<"enter 2nd string:";
  cin>>b;
  
    int first[26]={0},second[26]={0},i=0;
    while (a[i]!='\0')
    {
      first [a[i]-'a']++;
      i++;

    }
    i=0;
    while (b[i]!='\0')
    {
      second[b[i]-'a']++;
      i++;
    }
    for(i=0;i<26;i++)
    {
      if (first[i]!=second[i])
      {
       flag=1;
      }
    }

  if(flag==0)
  {
    cout<<"string entered are anagram";

  }
  else
  {
    cout<<"not anagram";
  }

}

Output:


Related Posts:

0 comments: