字母c代表什么数字
“adsfa” and “sdfac” are not anagrams
“ adsfa”和“ sdfac”不是字谜
如何检查两个字符串是否是字谜? (How to Check two Strings are Anagrams or not?)
So what we will do is find the frequency of each characters in first and second string and store it in two arrays. Now we will check the frequency of each character in two strings by comparing the two arrays. If every character has same frequency then the strings are anagrams otherwise not. Below I have written a C program to implement this logic. If you are finding any difficulty then comment below, I will try to solve your problem.
因此,我们要做的是找到第一个和第二个字符串中每个字符的频率并将其存储在两个数组中。 现在,我们将通过比较两个数组来检查两个字符串中每个字符的频率。 如果每个字符具有相同的频率,则字符串是字谜,否则不是。 下面,我编写了一个C程序来实现此逻辑。 如果您发现任何困难,请在下面评论,我将尽力解决您的问题。
C语言的Anagram程序 (Anagram Program in C)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//function to count the frequency of each character
void count_frequency(char str[],int s[])
{
int i=0,j,count;
while(str[i]!='\0')
{
j=0;
count=0;
while(str[j]!='\0')
{
if(str[i]==str[j])
count++;
j++;
}
s[str[i]-97]=count;
i++;
}
}
int main()
{
char str1[100],str2[100];
int i,j,flag=1,s1[26]={0},s2[26]={0};
printf("Enter first string:");
scanf("%s",str1);
printf("Enter second string:");
scanf("%s",str2);
if(strlen(str1)!=strlen(str2)) //if the lengths of two strings are not equal
{
printf("\nStrings are not anagrams");
exit(0);
}
count_frequency(str1,s1);
count_frequency(str2,s2);
for(i=0;i<26;++i) //checking freuency of each character
{
if(s1[i]!=s2[i])
{
flag=0;
break;
}
}
if(flag)
printf("\nStrings are anagrams");
else
printf("\nStrings are not anagrams");
return 0;
}
Output:
输出:
翻译自: https://www.thecrazyprogrammer.com/2015/03/c-program-to-check-two-strings-are-anagrams-or-not.html
字母c代表什么数字