功能
用到的觀念
CODE
#include<iostream>//header file
#include<iomanip>
#include<cmath>
using namespace std;
using std::setw;
using std::fixed;
using std::setprecision;
#include<string>
using std::string;
using std::getline;
int main()
{
int count=0,a,b=0;//declare int variable
string str;//declare string
cout<<"warning: please use proper grammer or it'll be wrong"<<endl;
cout<<"type the punctuation, you have to type after a word and then tpye
space"<<endl;
cout<<"for example: you, and me||I go; then I eat"<<endl;
cout<<"if you read the instruction press enter"<<endl;
system("pause");
system("cls");
cout<<"please enter a sentence and it will cut the sentence into
word"<<endl;//show information
getline(cin,str);//get the sentence
for(int i=0;i<=str.length();i++)//run the loop to print and calculate the
total
{
if(str[i]==' '||str[i]=='\0')//if it's space
{
b++;
}
if(str[i]!=' '&&str[i]!='\0'&&str[i]!='!'
&&str[i]!=','&&str[i]!=';'&&str[i]!='.')//if it's
not space
{
cout<<str[i];//print the word
count++;//counter plus 1
}
else if(str[i]==' '||str[i]=='!'||str[i]==','||str[i]==';')//if it's space
{
if(str[i-1]==' '||str[i-1]=='!'||str[i-1]==','||str[i-1]==';')
{
continue;
}
cout<<" the length is "<<count;//calculate the total of a word
cout<<endl;//newline
count=0;//make counter 0
}
if(str[i]=='\0')//if it's the end
{
if(str[i-1]=='!'||str[i-1]==','||str[i-1]==';'&&str[i]=='\0'||str[i]=='.')
{
continue;
}
cout<<" the length is "<<count;//show the how many a word
cout<<endl;
}
}
cout<<"the total word of a sentence: "<<b<<endl;//sjow the total words of
sentence
system("pause");
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.7.59