返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int n;
  7.     char str[100];
  8.     cout<<"請輸入任意字串(100字內): ";
  9.     cin.getline(str,100);
  10.     for(int i=0; str[i]!=NULL; i++)
  11.     {
  12.          if(int(str[i])==0)
  13.          {
  14.              n++;                  
  15.          }
  16.     }
  17.     cout<<"總共有"<<n+1<<"個單字"<<endl;
  18.     system("pause");  
  19.     return 0;   
  20. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int n=0;
  7.     char str[100];
  8.     cout<<"請輸入任意句子:";
  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. }
複製代碼

TOP

返回列表