Thứ Sáu, 29 tháng 8, 2014

Checking weather two strings are anagrams or not? in c++

Hi Guys,

Anagrams: An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once
preconditions:
1.both strings must have same length
2.each character in string is unique(no character must be repeated).

For instance   string1="sir", string2="sri"   these two strings are anagrams.
Solution:
#include<iostream.h>
#include<string.h>
#include<conio.h>
int main()
{
clrscr();
char arr[]="sri";
char arr1[]="sir";
int i,j,c=0;
int len1=strlen(arr);
int len2=strlen(arr1);
if(len1!=len2)
cout<<"Both Strings are not an anagram";
for(i=0;i<len1;i++)
{
 for(j=0;j<len2;j++)
 {
    if(arr[i]==arr1[j])
    {
    c++;
    }
 }
}
if(c==len1 && c==len2)
cout<<"given strings are anagrams";
else
cout<<"given strings are not anagrams";
return 0;
}


output:
-----------------------------




Không có nhận xét nào:

Đăng nhận xét