/*******************************************************
Write a method to decide if two strings are anagrams or not.
也就是说纯看字母出现次数咯?与位置无关,字符出现类型和次数相同则为anagram.
i.e. transpose letters字母完全不变,把字母位置进行调换,得到新的意思.
Note:
Mod Time: 4.20, 2012
Copyright: Ben
*******************************************************/
#include<stdio.h>
#include<stdlib.h>
#define STRING_MAX_LEN 10000
int main(void)
{
bool anagram;
char s1[STRING_MAX_LEN], s2[STRING_MAX_LEN];
char ch;
int StLen1 = 0, StLen2 = 0;
int BitMap1[128], BitMap2[128];
int i;
//initialize
for(i = 0; i < 128; i++)
{
BitMap1[i] = 0;
BitMap2[i] = 0;
}
freopen("String1.txt", "r", stdin);
while((ch = fgetc(stdin)) != EOF)
{
s1[StLen1++] = ch;
}
s1[StLen1] = '\0'; //whole string1 is read.
freopen("String2.txt", "r", stdin);
while((ch = fgetc(stdin)) != EOF)
{
s2[StLen2++] = ch;
}
s2[StLen2] = '\0'; //whole string2 is read.
//so far the 2 strings have been read from separate files.
#if 0
if(StLen1 != StLen2)
{
anagram = false;
}
else
#endif
{
//read and fill bitmaps. Maybe this can also be done in the reading process.
for(i = 0; i < StLen1; i++)
{
//if the char is not btw 0~127(ascii), DANGER of access violation!
BitMap1[s1[i]]++;
BitMap2[s2[i]]++;
}
anagram = true;
//FOR_LOOP:
for(i = 0; i < 128; i++)
{
if(BitMap1[i] != BitMap2[i])
{
//if((i != 10) && (i != 13) && (i != 32))
{
anagram = false;
break;
//break FOR_LOOP;
}
}
}
}
printf((anagram) ? ("They are anagram!\n") : ("Not anagrams!\n"));
return 0;
}
Write a method to decide if two strings are anagrams or not.
也就是说纯看字母出现次数咯?与位置无关,字符出现类型和次数相同则为anagram.
i.e. transpose letters字母完全不变,把字母位置进行调换,得到新的意思.
Note:
Mod Time: 4.20, 2012
Copyright: Ben
*******************************************************/
#include<stdio.h>
#include<stdlib.h>
#define STRING_MAX_LEN 10000
int main(void)
{
bool anagram;
char s1[STRING_MAX_LEN], s2[STRING_MAX_LEN];
char ch;
int StLen1 = 0, StLen2 = 0;
int BitMap1[128], BitMap2[128];
int i;
//initialize
for(i = 0; i < 128; i++)
{
BitMap1[i] = 0;
BitMap2[i] = 0;
}
freopen("String1.txt", "r", stdin);
while((ch = fgetc(stdin)) != EOF)
{
s1[StLen1++] = ch;
}
s1[StLen1] = '\0'; //whole string1 is read.
freopen("String2.txt", "r", stdin);
while((ch = fgetc(stdin)) != EOF)
{
s2[StLen2++] = ch;
}
s2[StLen2] = '\0'; //whole string2 is read.
//so far the 2 strings have been read from separate files.
#if 0
if(StLen1 != StLen2)
{
anagram = false;
}
else
#endif
{
//read and fill bitmaps. Maybe this can also be done in the reading process.
for(i = 0; i < StLen1; i++)
{
//if the char is not btw 0~127(ascii), DANGER of access violation!
BitMap1[s1[i]]++;
BitMap2[s2[i]]++;
}
anagram = true;
//FOR_LOOP:
for(i = 0; i < 128; i++)
{
if(BitMap1[i] != BitMap2[i])
{
//if((i != 10) && (i != 13) && (i != 32))
{
anagram = false;
break;
//break FOR_LOOP;
}
}
}
}
printf((anagram) ? ("They are anagram!\n") : ("Not anagrams!\n"));
return 0;
}