2016-12-01 23 views
0

我試圖顯示哪個字符以及它們中的每個字符在用戶選擇的句子中有多少個。所以如果用戶放入「Hello World!」程序應該返回一個字符和它使用的次數。統計數組中有多少個字符

" SPACE:1, !:1, H:1, W:1, e:1, d:1, l:3, o:2, r:1,"

我有它在開關,因爲我有其他選擇,用戶可以選擇。

現在我可以弄清楚使用什麼字符,它們中有多少是從空間到Q.我也可以把所有的小寫字母都去掉,但是如果它讀出一個'a',它會說有1' a'和一個SPACE(用ASCII碼從32開始,隨着小字母的增加而上升)。

這些是我使用的變量。

int menyval = 0, i, k = 0, h, j, count, count2; 
char input, str[100], getridof, add, character; 

這是我在這種情況下。

printf("Write a string not more then 50 chars:\n"); 
     getchar(); 
     i = 0; 
     j = 0; 
     count = 0; 
     int counts[50] = { 0 }; 
     gets(str); 
     str[j] = str[i]; 
      while (str[i] != '\0') { 


       if (str[i] >= 97 && str[i] <= 122) { 
        counts[str[i] - 97]++; 
       } 
       i++; 
       count++; 
      } 

      for (i = 0; i < 50; i++) { 
       if (counts[i] != 0) { 
        printf("%c: %d\n", i + 97, counts[i]); 
       } 
      } 

      while (str[j] != '\0') { 


       if (((str[j] >= 32 && str[j] <=96)) || ((str[j] >=123 && str[j] <= 126))) { 
        counts[str[j] - 32]++; 
       } 
       j++; 
      } 

      for (j = 0; j < 50; j++) { 
       if (counts[j] != 0) { 
        //if((j) < 127) 
        printf("%c: %d\n", j + 32, counts[j]); 
       } 
      } 
     printf("Total amount of char: %d\n", count); 
     str[i] = '\0'; 
     system("pause"); 
     system("cls"); 

這是一個作業,讓我明白了,如果你不想說的直接代碼,但我會非常心存感激的一些提示點我在正確的方向。

+1

請勿使用'gets()'.http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – MrMuMu

+0

您的'count'數組不夠大,無法保存您嘗試存儲在具有'j'的循環中的數值。你在數組中只有50個整數槽,但是你的索引要遠遠超過那個('96-32 = 64')。在打印和嘗試再次使用它時,您也不會重新調整陣列的大小。我假設你真的想這樣做。 – eddiem

+0

使用地圖可以完成清潔實現。請檢查地圖文檔。 – Ehsan

回答

1

ACII表:http://www.asciitable.com/

char str[12] = "hello world"; 

    // initialize an array of each possible character 
    int charCount[128]; 
    memset(charCount, 0, sizeof(charCount)); 

    // iterate through the array of characters 
    // incrementing the index in charCount matching the element in str 
    char* currChar = str; 
    while(*currChar) 
      ++charCount[*(currChar++)]; 

    // iterate through the array once more 
    for(int i = 0; i < 128; ++i) { 
      // if the character was found in the string, 
      // print it and its count 
      if(charCount[i]) { 
        printf("%c: %d\n",i,charCount[i]); 
      } 
    } 
1

我有點糾正和清除在此方面自己的代碼:

  1. 未使用的變量的刪除聲明。
  2. 將所有聲明置頂
  3. 刪除無用命令。
  4. 更改「神奇」數爲6597的符號'A''a' - 是的,char小號數字。
  5. 評論代碼的個別部分。
  6. 和 - 校正誤差,主要是 - 當然:
    1. 正在重置計數器
    2. 分裂不連續範圍符號(||在原始狀態)轉換成2 連續

所以完整的代碼是現在:

#include <stdio.h> 

int main() { 
    int i, count = 0, counts[50] = { 0 }; 
    char str[100]; 

    printf("Write a string not more than 50 chars:\n"); 
    gets(str); 

    /* Counting capital letters and all symbols, too*/ 
    i = 0; 
    while (str[i] != '\0') { 
     if (str[i] >= 'A' && str[i] <= 'Z') { 
      counts[str[i] - 'A']++; 
     } 
     i++; 
     count++; 
    } 

    /* ... and printing results */ 
    for (i = 0; i < 50; i++) { 
     if (counts[i] != 0) { 
      printf("%c: %d\n", i + 'A', counts[i]); 
     } 
    } 

    /* ... and clear the counter */ 
    for (i = 0; i < 50; i++) 
     counts[i] = 0; 

    /* Counting small letters */ 
    i = 0; 
    while (str[i] != '\0') { 
     if (str[i] >= 'a' && str[i] <= 'z') { 
      counts[str[i] - 'a']++; 
     } 
     i++; 
    } 

    /* ... and printing results */ 
    for (i = 0; i < 50; i++) { 
     if (counts[i] != 0) { 
      printf("%c: %d\n", i + 'a', counts[i]); 
     } 
    } 

    /* ... and clear the counter */ 
    for (i = 0; i < 50; i++) 
     counts[i] = 0; 

    /* Counting symbols between SPACE and 'A' */ 
    i = 0; 
    while (str[i] != '\0') { 
     if ((str[i] >= ' ' && str[i] < 'A')) { 
      counts[str[i] - ' ']++; 
     } 
     i++; 
    } 

    /* ... and printing results */ 
    for (i = 0; i < 50; i++) { 
     if (counts[i] != 0) { 
      printf("%c: %d\n", i + ' ', counts[i]); 
     } 
    } 

    /* ... and clear the counter */ 
    for (i = 0; i < 50; i++) 
     counts[i] = 0; 

    /* Counting symbols over 'z' */ 
    i = 0; 
    while (str[i] != '\0') { 
     if ((str[i] >= 123 && str[i] <= 126)) { 
      counts[str[i] - 123]++; 
     } 
     i++; 
    } 

    /* ... and printing results */ 
    for (i = 0; i < 50; i++) { 
     if (counts[i] != 0) { 
      //if((i) < 127) 
      printf("%c: %d\n", i + 123, counts[i]); 
     } 
    } 


    printf("Total amount of char: %d\n", count); 
    str[i] = '\0'; 
    system("pause"); 
    system("cls"); 
    return 0; 
} 

我測試了它,現在它工作正常 - 儘管它仍然是醜陋的。但它主要是你的代碼,所以你會瞭解它

+0

請勿使用'gets'。並刪除'str [i] ='\ 0';' – BLUEPIXY

相關問題