Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在字符串中打印重复的字母,并将其计数用C表示?_C_C Strings - Fatal编程技术网

如何在字符串中打印重复的字母,并将其计数用C表示?

如何在字符串中打印重复的字母,并将其计数用C表示?,c,c-strings,C,C Strings,我一直在使用一种算法来计算和打印C字符串中的重复字母(两次或更多) 例如: 如果输入字符串为:“你好” 输出应为: e - 3 h - 2 l - 2 我的当前代码已经打印了我需要的,但是它不考虑大写字母< /强>,它也不断给我消息“代码>堆栈粉碎检测(内核转储)< /代码>。因此,它不能正常工作,我不知道为什么: #include <stdio.h> #include <string.h> int main() { char string[10]; in

我一直在使用一种算法来计算和打印C字符串中的重复字母(两次或更多)

例如: 如果输入字符串为:
“你好”
输出应为:

e - 3
h - 2
l - 2

我的当前代码已经打印了我需要的,但是它<强>不考虑大写字母< /强>,它也不断给我消息“代码>堆栈粉碎检测(内核转储)< /代码>。因此,它不能正常工作,我不知道为什么:

#include <stdio.h>
#include <string.h>

int main()
{
   char string[10];
   int c = 0, count[26] = {0};

   printf("Enter a string of size [10] or less:\n");
   gets(string);

   while (string[c] != '\0')
   {
      /**reading characters from 'a' to 'z' or 'A' to 'Z' only
          and ignoring others */

      if ((string[c] >= 'a' && string[c] <= 'z') || (string[c] >= 'A' && string[c] <= 'Z'))

      {
         if (string[c] >= 'a' && string[c] <= 'z')
         {
         count[string[c]-'a']++;
         }
         else if (string[c] >= 'A' && string[c] <= 'Z')
         {
         count[string[c]-'A']++;
         }
      }

      c++;
   }

   for (c = 0; c < 26; c++)
   {
      /** Printing only those characters
          whose count is at least 2 */

      if (count[c] > 1)
         printf("%c - %d \n",c+'a',count[c]);
   }

   return 0;
}
#包括
#包括
int main()
{
字符串[10];
int c=0,计数[26]={0};
printf(“输入一个小于等于[10]的字符串:\n”);
获取(字符串);
while(字符串[c]!='\0')
{
/**仅从“a”到“z”或从“a”到“z”读取字符
忽视别人*/
如果((string[c]>='a'&&string[c]='a'&&string[c]='a'&&string[c]='a'&&string[c]='a'&&string[c]1)
printf(“%c-%d\n”,c+'a',计数[c]);
}
返回0;
}
“你好”
不适合大小为
10
的字符数组。这是一个很好的例子,说明了为什么不应该使用
gets

使用:

fgets(string, sizeof(string), stdin);