问题描述
  Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的。例如,“Unclear”和“Nuclear”、“Rimon”和“MinOR”都是Anagrams。编写一个程序,输入两个单词,然后判断一下,这两个单词是否是Anagrams。每一个单词的长度不会超过80个字符,而且是大小写无关的。
  输入格式:输入有两行,分别为两个单词。
  输出格式:输出只有一个字母Y或N,分别表示Yes和No。
  输入输出样例
样例输入
Unclear
Nuclear
样例输出
Y
 
 
 /*
思路:都转换成小写,在长度相等的情况下,
分别遍历记录下每个字母出现的次数。
最后比较各个数字是否相等。 字符串数组长度:100 ——单词长度不会超过80。
*/
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<ctype.h>//C语言转换大小写头文件
int main(){
char c1[],c2[];//定义字符串数组
int a[],b[];//记录出现次数
memset(a,,sizeof(a));
memset(b,,sizeof(b));
scanf("%s\n%s",c1,c2);
int len1,len2;
len1=strlen(c1);//求字符串长度
len2=strlen(c2);
int i; if(len1 != len2){
printf("N");
}else{
/*遍历c1*/
for(i = ; i < len1; i++){
// str1[i] = tolower(c1[i]);
if(c1[i] == 'a'||c1[i] == 'A')
a[]++;
else if(c1[i] == 'b'||c1[i] == 'B')
a[]++;
else if(c1[i] == 'c'||c1[i] == 'C')
a[]++;
else if(c1[i] == 'd'||c1[i] == 'D')
a[]++;
else if(c1[i] == 'e'||c1[i] == 'E')
a[]++;
else if(c1[i] == 'f'||c1[i] == 'F')
a[]++;
else if(c1[i] == 'g'||c1[i] == 'G')
a[]++;
else if(c1[i] == 'h'||c1[i] == 'H')
a[]++;
else if(c1[i] == 'i'||c1[i] == 'I')
a[]++;
else if(c1[i] == 'j'||c1[i] == 'J')
a[]++;
else if(c1[i] == 'k'||c1[i] == 'K')
a[]++;
else if(c1[i] == 'l'||c1[i] == 'L')
a[]++;
else if(c1[i] == 'm'||c1[i] == 'M')
a[]++;
else if(c1[i] == 'n'||c1[i] == 'N')
a[]++;
else if(c1[i] == 'o'||c1[i] == 'O')
a[]++;
else if(c1[i] == 'p'||c1[i] == 'P')
a[]++;
else if(c1[i] == 'q'||c1[i] == 'Q')
a[]++;
else if(c1[i] == 'r'||c1[i] == 'R')
a[]++;
else if(c1[i] == 's'||c1[i] == 'S')
a[]++;
else if(c1[i] == 't'||c1[i] == 'T')
a[]++;
else if(c1[i] == 'u'||c1[i] == 'U')
a[]++;
else if(c1[i] == 'v'||c1[i] == 'V')
a[]++;
else if(c1[i] == 'w'||c1[i] == 'W')
a[]++;
else if(c1[i] == 'x'||c1[i] == 'X')
a[]++;
else if(c1[i] == 'y'||c1[i] == 'Y')
a[]++;
else if(c1[i] == 'z'||c1[i] == 'Z')
a[]++;
} /*遍历c2*/
for(i = ; i < len2; i++){
// c2[i] = tolower(c2[i]);
if(c2[i] == 'a' || c2[i] == 'A')
b[]++;
else if(c2[i] == 'b'||c2[i] == 'B')
b[]++;
else if(c2[i] == 'c'||c2[i] == 'C')
b[]++;
else if(c2[i] == 'd'||c2[i] == 'D')
b[]++;
else if(c2[i] == 'e'||c2[i] == 'E')
b[]++;
else if(c2[i] == 'f'||c2[i] == 'F')
b[]++;
else if(c2[i] == 'g'||c2[i] == 'G')
b[]++;
else if(c2[i] == 'h'||c2[i] == 'H')
b[]++;
else if(c2[i] == 'i'||c2[i] == 'I')
b[]++;
else if(c2[i] == 'j'||c2[i] == 'J')
b[]++;
else if(c2[i] == 'k'||c2[i] == 'K')
b[]++;
else if(c2[i] == 'l'||c2[i] == 'L')
b[]++;
else if(c2[i] == 'm'||c2[i] == 'M')
b[]++;
else if(c2[i] == 'n'||c2[i] == 'N')
b[]++;
else if(c2[i] == 'o'||c2[i] == 'O')
b[]++;
else if(c2[i] == 'p'||c2[i] == 'P')
b[]++;
else if(c2[i] == 'q'||c2[i] == 'Q')
b[]++;
else if(c2[i] == 'r'||c2[i] == 'R')
b[]++;
else if(c2[i] == 's'||c2[i] == 'S')
b[]++;
else if(c2[i] == 't'||c2[i] == 'T')
b[]++;
else if(c2[i] == 'u'||c2[i] == 'U')
b[]++;
else if(c2[i] == 'v'||c2[i] == 'V')
b[]++;
else if(c2[i] == 'w'||c2[i] == 'W')
b[]++;
else if(c2[i] == 'x'||c2[i] == 'X')
b[]++;
else if(c2[i] == 'y'||c2[i] == 'Y')
b[]++;
else if(c2[i] == 'z'||c2[i] == 'Z')
b[]++;
}
for(i=;i<;i++){
if(a[i] != b[i]){
printf("N");
break;
}
}
if(i>=){
printf("Y");
}
}
return ;
}
04-05 03:50