Programmer

Tuesday 12 June 2018

Check The String is Anagram or Not.

What is Anagram?
C program to check whether two strings are anagrams or not, a string is assumed to consist of lower case alphabets only. Two words are said to be anagrams of each other if the letters of one word can be rearranged to form the other word. So, in anagram strings, all characters occur the same number of times. For example, "abc" and "cab" are anagram strings, as every character 'a,' 'b,' and 'c' occur the same number of times (one time here) in both the strings. A user will input two strings, and our algorithm counts how many times each character ('a' to 'z') appear in both the strings and then compare their corresponding counts. The frequency of an alphabet in a string is how many times that alphabet appears in the string. For example, the frequency of 'm' in the string "programming" is '2' as it is present two times in "programming."

//Program to check The string is Anagram or not.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char ch1[100],ch2[100],temp;
int i,flag=1,str1[26],str2[26];
clrscr();
cout<<"Enter 1st String :";
cin.getline(ch1,100);
int n=strlen(ch1);
for(i=0;i<26;i++)
{
str1[i]=0;
}
for(i=0;ch1[i]!='\0';i++)
{
if(islower(ch1[i]))
ch1[i]=toupper(ch1[i]);
}
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(ch1[i]>ch1[j])
{
temp=ch1[i];
ch1[i]=ch1[j];
ch1[j]=temp;
}
}
}
cout<<"Enter 2nd String :";
cin.getline(ch2,100);
int s=strlen(ch2);
for(i=0;i<26;i++)
{
str2[i]=0;
}
for(i=0;ch2[i]!='\0';i++)
{
if(islower(ch2[i]))
ch2[i]=toupper(ch2[i]);
}
for(i=0;i<s;i++)
{
for(int j=i+1;j<s;j++)
{
if(ch2[i]>ch2[j])
{
temp=ch2[i];
ch2[i]=ch2[j];
ch2[j]=temp;
}
}
}
if(strcmp(ch1,ch2)==0)
cout<<"Both Strings Are Anagram.";
else
cout<<"Both Strings Are Different.";
getch();

}


No comments:

Post a Comment

Program To Print Particular Line From File.

#include<fstream.h> #include<conio.h> #include<stdlib.h> void main() { ifstream fin; int count=0,num,l=1; char ...