2011-05-12 1 views
1

可能性の重複:
c program for 20 chars and reports quantity of each[SOLVED]文字列を読み込み、各文字が何回出現するかを示すCプログラムを作成しますか?

これは私がこれまで持っているものです。しかし、私がプログラムを使用して「Qwerty」と入力すると、Qを5回使用したというメッセージが表示されます。助けてください。それは常に文字列の最初の文字だから

c=*line; 

#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); 
} 
+0

あなたはそれを読みやすいようにフォーマットできますか?ブロッククォート機能を使用します。 –

+0

sozがあなたのために編集しました – killerbill09

+1

[20文字の文字列を読み込み、各文字が何回出現するかを示すCプログラムを作成しますか?](http://stackoverflow.com/questions/5975413/create-ac-プログラムでは、20文字の文字列と数えられる多くの文字列)と[Cプログラミングの質問?](http://stackoverflow.com/questions/5978708/c-プログラミングの質問) –

答えて

3

問題がです。それを変更してみてください:

c=line[i]; 
+0

私は上記の1つを使用していますが、これもtyで動作します。 – killerbill09

2

問題はここにある:

cは常にあなたが qwertyを入力するときに、出力として Q 5を参照してください理由である入力で 最初手紙を、持っています
c = *line; 

cは、入力文字列のi番目の文字を持っています

c = *(line+i); 

:へ

変更に。

+0

ありがとうございました:D – killerbill09

関連する問題