2011-11-14 10 views
2

私はプログラミングの完全な初心者です。私が知っていることには非常に基本的な問題がたくさんあります。ユーザーが単語を入力するプログラムを作成し、whileループを使用して単語の文字数を計算し、結果を画面に表示する必要があります。C言語では、whileループを使用して、ユーザーが入力する文字数をカウントします。

ユーザーが単語を入力しても問題はありませんが、問題はwhileループにあります。私はそれをコード化する方法を理解できません。私は本当にこれにいくつかの助けに感謝します。

おかげ

編集:

#include <stdio.h> 
#include <string.h> 

int main(void) 
{ 
char input[30]; 
int wordlen; 

printf("please enter a word: \n"); 
scanf("%29c", input); 

while (input < 30); 
{ 
/*Not sure what to put in here*/ 
printf("Number of letters in input is %s", /*???*/); 
} 

return 0; 
} 

を別の編集:これは宿題ですが、私の講師はゴミで、よく物事を説明していません

ここで私がこれまで行ってきたものです。私は学びたいと思っていて、それがどのように機能するのか理解したいのですが、必ずしも直接的な答えを期待しているわけではありません。自分でそれを解決する方法についてのヒントさえも素晴らしいだろう。ありがとう


ここで私は多くの試行錯誤の後で私が思い付いたものです。私はそれが正しいと思うが、あなたの意見が欲しい。私は3週間も経たないうちにCをやっているので、私のテクニックは貧弱かもしれないことに気をつけてください。みなさんのご意見ありがとうございます。あなたが文字配列内の文字列を格納している場合

#include <stdio.h> 
#include <string.h> 

int main(void) 
{ 
char input[30]; 
int i; 
int x; 
x=0; 

printf("please enter a word: \n"); 
scanf("%29s", input); 
i=strlen(input); 

while (x < i) 
{ 
x++; 
} 
printf("Number of letters in input is %d", x); 


return 0; 
} 
+1

は、問題を解決するためにいくつかの努力/試みを表示します。 –

+0

私たちがあなたを助けることができるように、あなたが理解していないものをより具体的にするようにしてください(あなたが書いたコードが常に良いことを示しています)。 –

+2

これは宿題のタグ付けが必要だと思う。 – kbyrd

答えて

4

、 私'

Cの文字列はNULLで終了します。つまり、char [30]によって指される "qW3rTy"のような文字列がメモリ上にある場合、これは実際には次のようになりますこの:

input[0] = 'q' 
input[1] = 'w' 
input[2] = '3' 
input[3] = 'r' 
input[4] = 'T' 
input[5] = 'y' 
input[6] = '\0' 
... // It doesn't matter what's after here, because the NULL above marks the end. 

'\0'は、そのバイトの値がNULL別名、文字通りゼロであることの別の言い方であること。したがって、文字列内の文字数をカウントするには、最初のヌル文字を探して文字列をループし、カウンタをインクリメントします。 NULLを数えないようにしてください。

0

、sが言う、最も簡単な解決策は、文字列の文字数を計算することができ、strlen関数と呼ばれるC-機能が内蔵され使用することです

printf("%d\n",strlen(str)); 

あなたがNULLに到達するまで、whileループを使用して、あなたは(「\ 0」)配列の先頭からチェックする必要があるようなものだ文字、:homeworkタグ付きポストの精神で

len = 0; 
while(str[len] != '\0') 
    len++; 
printf("%d\n",len); 
-1
#include <stdio.h> 
#include <string.h> 

int main(void){ 
    char input[30]; 
    int wordlen; 

    printf("please enter a word: \n"); 
    // %29c is request 29 chars include newline 
    // %29s is max 29 chars input exclusive space chars(space, tab, newline..) 
    // %*c is drop out char for newline ('\n' remain buffer) 
    // %s is request of C string 
    // C string is char sequence , and last char is '\0' 
    scanf("%29s%*c", input); 
    // strlen is counting C string chars until last '\0' char 
    wordlen = strlen(input); 

    //while(condition); //this while does not has block to execute 
    //input is constant address , The address comparison(input < 30) is meaningless 
    //while break condition as 0==strlen(input) is NG (why reason scanf wait until input) 
    //exit loop when input special string "-quit" 
    while (0!=strcmp("-quit", input)){ 
     printf("Number of letters in input is %d.\n", wordlen); 
     printf("please enter a word: \n"); 
     scanf("%29s%*c", input); 
     wordlen = strlen(input); 
    } 

    return 0; 
} 
+1

これは、コードブロックをコメントで隠すのではなく、説明付きで分割した方がずっと読みやすくなります。また、質問が「宿題」とタグ付けされているときに、コードを譲るだけのヒントが好まれます。 –

0
i = strlen(input); 
while (x < i){ 
    x++; 
} 

あなたは意味がありませんstrlen関数と併せて、xの数をカウントアップすることができます。 x = iだけです。 自分自身をカウントする目的がstrlenのない実際の文字を数えたい場合。

EX。)

while (input[x] != '\0'){ 
    x++; 
} 
関連する問題