返回列表 發帖

[作業] 判斷句子中有幾個單字

本帖最後由 tonyh 於 2014-3-1 14:26 編輯

設計一程式, 讓使用者輸入任一句子, 甚至是一篇文章, 電腦判斷該句子或文章中有幾個單字.

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int n=0;
  7.     char str[100];
  8.     cout<<"請輸入任一句子(將判斷句子中有幾個單字): "<<endl;
  9.     cin.getline(str,100);
  10.     for(int i=0; str[i]!=NULL; i++)
  11.     {
  12.         if((int(str[i])>=65 && int(str[i])<=90)||(int(str[i])>=97 && int(str[i])<=122))
  13.         {
  14.             n++;
  15.             if((int(str[i+1])>=65 && int(str[i+1])<=90)||(int(str[i+1])>=97 && int(str[i+1])<=122))
  16.                 n--;     
  17.         }      
  18.     }
  19.     cout<<endl<<"您輸入的句子中, 包含了"<<n<<"個單字"<<endl;
  20.     system("pause");  
  21.     return 0;   
  22. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

返回列表