可能重複:
c program for 20 chars and reports quantity of each[SOLVED]創建一個讀取字符串並指出每次字符發生次數的C程序?
這是我到目前爲止所。但是,當我使用該程序並說我輸入「qwerty」時,它只是說我已經使用過5次Q.請幫忙。
#include <stdio.h>
#include <string.h>
void readData(char line[], int size)
{
printf("Enter a string no longer than 20 characters: ");
fgets(line, size, stdin);
line[strlen(line) - 1] = '\0';
strupr(line);
printf("You Entered %s\n", line);
return;
}
void counter(char line[], int counts[])
{
int i;
char c;
for (i = 0; i < strlen(line); i++)
{
c = *line;
if (c >= 'A' && c <= 'Z')
{
counts[c - 'A']++;
}
else
{
counts[26]++;
}
}
return;
}
void printer(int counts[])
{
int i;
for (i=0; i < 26; i++)
{
printf("there are %d occurances of %c\n", counts[i], i + 'A');
}
printf("there are %d occurances of non alphabetic\n", counts[26]);
return;
}
int main()
{
const int SIZE = 22;
char line[22] = {'\0'};
int counts[27] = { 0 };
readData(line, SIZE);
counter(line, counts);
printer(counts);
return(0);
}
你能格式化它,所以它是可讀的嗎?使用Blockquote功能。 – 2011-05-12 15:03:23
soz爲你編輯 – killerbill09 2011-05-12 15:06:29
[創建一個讀取20個字符的字符串並指出每個字符出現的次數的C程序的完全重複](http://stackoverflow.com/questions/5975413/create-ac-程序在一個字符串中讀取20個字符和狀態多麼多)和[C編程問題?](http://stackoverflow.com/questions/5978708/c-編程問題) – 2011-05-12 15:29:17