- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int n=0;
- char str[100];
- cout<<"請輸入任意句子:";
- cin.getline(str,100);
- for(int i=0; str[i]!=NULL; i++)
- {
- if(int(str[i])>=65 && int(str[i])<=90 || (int(str[i])>=97 && int(str[i])<=122)
- {
- n++;
- if(int(str[i+1])>=65 && int(str[i+1])<=90 || (int(str[i+1])>=97 && int(str[i+1])<=122)
- n--;
- }
- }
- cout<<endl<<"您輸入的句子有"<<n<<"個單字。"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |