2014-05-19 104 views
-3

所以,在這個程序中,我們必須計算數組中的字母字符?

A.計算一個字符數組中發現的每個字母的數量和這些計數存儲在一個整數數組

我理解這一部分,但它說,我們必須使用混淆了我的ASCII值。

這是爲參考文件:

" A frog walks into a bank and asks the teller, Miss Pattywack 
    for a loan. Miss Pattywack tells the frog, 
    "you must have collateral for the loan". 
    The frog pulls out of his pouch his porcelain people collection 
    and offers them as collateral. 
    Miss Pattywack looks at the figurines and tells the frog that 
    the collection is not acceptable. 
    The bank manager overhears the conversation and yells to the 
    teller - "It's a nick nack Pattywack, give the frog a loan!" 

使用for循環來檢查字符數組

i的每個字符。使用toupper來處理這封信,所以你只處理大寫字母

ii。如果字符是字母表中的字母,在字符的ASCII值減去65

1)65的位置遞增整數數組是字母的ASCII值,「A」

(1)如果該字符是要增加的字符A,65-65 = 0的位置A

(2)如果該字符是C,67-65 = 2位置要增加字符C

我有這個至今:

void CountAlphabetCharacters(char chArray[MAX_CHARACTERS], int lengthOfArray) 
{ 
     int index; 

     for(index = 0; index <= lengthOfArray; index++) 
     { 
      chArray[index] = toupper(chArray[index]); 

       static_cast<int>(index); 

     } 
} 

這就是我的全部,因爲這就是我理解。我的意思是,我明白你是如何得到ASCII值的,但我對如何真正爲此做for循環感到失落。就像我假設你看到文件中的字符,但我不明白你是如何得到你的價值,並繼續前進的。我不知道我是否有道理,但我希望我能做,有人可以幫助!提前致謝。

+1

所以你基本上是問如何從文件中讀取文本到字符數組?您可以檢查[C++ I/O庫](http://en.cppreference.com/w/cpp/io)的功能。 –

+0

我已經將文本文件讀入數組了。我需要重新計算文件中字母表中每個字母出現的次數。但是,使用ASCII碼值65 – letsgetkrejci

+0

然後我不明白你在問什麼: -/... –

回答

0

你需要的東西:

  1. 一個數組來保存你讀的字符。我認爲chArray就是那個數組。
  2. 一個數組來保存字母表的字母數。我看不到那個數組。
  3. 更新CountAlphabetCharacters中的代碼以遍歷chArray中的字符並更新chArray中的字母數。

在致電CountAlphabetCharacters之前,請創建用於保存字符數的數組。

int charCountArray[26] = {0}; 

更改功能CountAlphabetCharacters接受它作爲參數。

void CountAlphabetCharacters(char chArray[MAX_CHARACTERS], 
          int charCountArray[], 
          int lengthOfArray) 

在調用CountAlphabetCharacters時使用它作爲參數。

更新CountAlphabetCharacters的實施。

void CountAlphabetCharacters(char chArray[MAX_CHARACTERS], 
          int charCountArray[], 
          int lengthOfArray) 
{ 
    int index; 
    int ch; 
    for(index = 0; index <= lengthOfArray; index++) 
    { 
     // The character at the index. 
     ch = chArray[index]; 

     // Check whether it is an alphabetical character. 
     if (isalpha(ch)) 
     { 
     // If so, make it the uppercase letter. 
     ch = toupper(ch); 

     // Increment the charCountArray for the letter. 
     charCountArray[ch-'A']++; 
     } 
    } 
} 
+0

這有一點幫助,我可能不得不改變一些它適合,但這有助於我走向正確的方向。謝謝! – letsgetkrejci

0
void CountAlphabetCharacters(char chArray[MAX_CHARACTERS], int lengthOfArray) 
{ 
    int index; 
    int counter[26]={0}; // holds occurrence of each character 
    for(index = 0; index < lengthOfArray; index++) 
    { 
     if(isalpha(chArray[index]){ 
     int val = (int)toupper(chArray[index]); 
     counter[val-65]++; //increment occurrence count for this character 
     } 
    } 

    for(char c ='A',index = 0; index <26; index++,c++) 
    { 
     printf("%c : %d", c, counter[index]); //print character and corresponding occurrence 
    } 
} 
+1

for(char c ='A',index = 0; index <= 26; index ++,C++)應該改變,我認爲要「指數<26」。類似地,對於「index <= lengthOfArray」。它們可能會導致內存溢出。 – tpb261

+0

@ user2702245,複製粘貼效果:D。謝謝。 – Rakib

+0

謝謝你的幫助! – letsgetkrejci