天天看點

【哈工大C作業實驗】:13-1作業題題目思路代碼

題目

輸入一行字元,統計其單詞的個數,單詞之間用空格進行分隔。

輸入要求:讀入的字元串應包含有空格

輸出要求:”There are is %d in teh line.\n”

輸入輸出樣例:

Input sample:

The C program language.

Output sample:

There are is 4 in teh line.

思路

單詞個數為空格數+1,但是如果在第一個單詞前出現空格的話那個空格需要剔除。

代碼

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int i,count=;
    char str[];
    gets(str);
    for(i=;i<strlen(str);i++){
        if(str[i]==' '&&i!=){
            count++;
        }
    }
    printf("There are is %d in teh line.\n",count+);
    return ;
}
           

繼續閱讀