[英]Counting frequency of a character returns 0 always
我寫了一個 C function ,它將計算 char 數組中每個字符的頻率。 該程序有效,但是當我使用給定的測試代碼運行它時,頻率為 0,但是如果我要在自己的解決方案中運行它,我會得到正確的答案。
這是我的代碼。
void countchars(const char *array, unsigned int *counts)
{
int c = 0;
int k = 0;
while (array[c] != '\0')
{
if (array[c] >= 'a' && array[c] <= 'z' )
{
counts[array[c]-'a']++;
}
c++;
if ( array[k] >= 'A' && array[k] <= 'Z' )
{
counts[array[c]-'A']++;
}
k++;
}
}
這是給我的打印 function。
void printcounts(int min, int max, const unsigned int *counts) {
for (int i = min; i <= max; i++) {
printf("%c: %u --- ", i, counts[i]);
if ((i - min + 1) % 6 == 0)
printf("\n");
}
}
這是使用它們的主要 function。
int main(void)
{
unsigned int counts[256] = { 0 };
char sample[] = { 'a','b','r','a','c','a','d','a','b','r','a',0 };
countchars(sample, counts);
printcounts('a', 'z', counts);
return 0;
}
這是使用給定打印計數時得到的 output。
a: 0 --- b: 0 --- c: 0 --- d: 0 --- e: 0 --- f: 0 ---
g: 0 --- h: 0 --- i: 0 --- j: 0 --- k: 0 --- l: 0 ---
m: 0 --- n: 0 --- o: 0 --- p: 0 --- q: 0 --- r: 0 ---
s: 0 --- t: 0 --- u: 0 --- v: 0 --- w: 0 --- x: 0 ---
y: 0 --- z: 0 ---
這是預期的 output
a: 5 --- b: 2 --- c: 1 --- d: 1 --- e: 0 --- f: 0 ---
g: 0 --- h: 0 --- i: 0 --- j: 0 --- k: 0 --- l: 0 ---
m: 0 --- n: 0 --- o: 0 --- p: 0 --- q: 0 --- r: 2 ---
s: 0 --- t: 0 --- u: 0 --- v: 0 --- w: 0 --- x: 0 ---
y: 0 --- z: 0 ---
在 printcounts function 中, counts[]
在printcounts('a', 'z', counts);
而頻率從counts[0]
開始存儲。
大寫和小寫字母的頻率存儲在相同的數組位置。
您已經使用了一個足夠大的數組來存儲每個 ASCII 字符的頻率。
unsigned int counts[256] = { 0 };
現在你所要做的就是在每次遇到一個字符時將counts[character]
增加 1。 無需定義要使用的字符范圍並從counts[0]
開始填充。
注意:這將計算所有ASCII 字符的頻率。
#include <stdio.h>
void countchars(const char *array, unsigned int *counts)
{
int c = 0;
while (array[c] != '\0')
{
++counts[(unsigned char)array[c]];
c++;
}
}
void printcounts(int min, int max, const unsigned int *counts)
{
for (int i = min; i <= max; i++)
{
printf("%c: %u --- ", i, counts[i]);
if ((i - min + 1) % 6 == 0)
printf("\n");
}
}
int main(void)
{
unsigned int counts[256] = { 0 };
char sample[] = { 'a','b','r','a','c','a','d','a','b','r','a',0 };
countchars(sample, counts);
printcounts('a', 'z', counts);
return 0;
}
您提供的打印 function 預計任何給定字符本身就是相應計數的索引,但這不是您的countchars
function 存儲計數的方式。 相比
counts[array[c]-'a']++;
至
printf("%c: %u --- ", i, counts[i]);
. 既然你說打印 function 是給你的,我想你是不允許修改它的。 在這種情況下,您必須更新countchars
以將每個字符的計數存儲在正確的索引處。 看起來,對於小寫字母,它是array[c]
,而不是array[c]-'a'
。
問題是您將值寫入counts
數組而不是讀取,因為它們位於數組中的不同位置。
例如,數組中處理的第一個字符是“a”。 並執行以下代碼:
counts[array[c]-'a']++;
array[c]
的 char 值是 'a'(在 ASCII 中是 97)。 從“a”中減去“a”(或從 97 中減去 97)將得到 0。因此counts[0]
現在增加到 1 的值。
在您提供的printcounts
function 中,您從不查看counts[0]
,因為您傳入的最小值是“a”,即 97。
在編寫代碼時,您將從索引 0 到 25 counts
counts
數值遞增,但在調用printcounts('a', 'z', counts);
有了這個:
counts[array[c]-'a']++;
你正在減去一個偏移量('a')。 沒有偏移量。 改用這個:
counts[array[c]]++;
你的第二個if
是錯誤的。 您不需要任何k
變量,只需在增加它之前使用i
。
我認為你必須初始化一個變量來存儲字符的頻率
string frequency[26];
for (int i = 0; i < 26; i++)
{
frequency[i] = 0;
}
然后,迭代一個字符串來計算字符的頻率
for (int j = 0; array[j] != '\0'; j++)
{
if (isupper(array[j] != 0)
{
frequency[array[j] - 65]++;
}
else if (islower(array[j] != 0)
{
frequency[array[j] - 97]++;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.