C 單詞首字母大寫&統計單詞個數
阿新 • • 發佈:2019-02-08
#include <string.h> #include <stdio.h> int main(int argc, const char * argv[]){ /* * 單詞首字母大寫&統計單詞個數 */ //定義變數 char str[100]; int words = 0; int count = 0; //統計單詞個數 //提示輸入字串 printf("請輸入一個字串;\n"); //接收並儲存到陣列 gets(str); //迴圈取出每個字元 遇到\0迴圈結束 for (int i=0; str[i]!='\0'; i++) { //判斷 if(str[i]==' ') { //把是否是單詞標記改一下 words = 0; //這是一個標記,words = 0表示是一個單詞 } else if (words == 0){ // 當前迴圈 字元時空格 下次迴圈一定是一個單詞 count ++; str[i] = str[i] - 32;//大寫 words = 1; //讓單詞標誌變為不是單詞 } } printf("單詞個數:%d\n字串: %s\n", count, str); puts(str); return 0; }