编写C++风格程序,输入一行文本,统计文本中出现的空格数、标点符号数和单词数;

感谢
是统计单词数,不是字母个数

代码贴上来了。

下次记得自己写,程序并不难,不要老想着让别人帮忙写。

#include <cstdlib>

#include <iostream>

#include <string>

using namespace std;

int main(int argc, char *argv[])

{

    int spaceNum=0, markNum=0, words=0;

    string str;

    cout<<"Please input a string: ";

    getline(cin, str, '\n');

    cout<<endl<<str<<endl;

    

    bool isWord = true;

    

    for(int i=0; i < str.length(); i++)

    {

        if(str[i] == 0x20)

        {

             spaceNum++;

             isWord= true;

        }

        else if(str[i] > 'A' && str[i] < 'z' )

        {

             if(isWord)

             {

                 isWord = false;

                 words++;

             }

         }

         else

         {

             markNum++;

             isWord = true;

         }

    }

    

    cout<<"\nspaceNum= "<<spaceNum<<" ; markNum= "

        <<markNum<<" ; words= "<<words<<endl;

    system("PAUSE");

    return EXIT_SUCCESS;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-03-05
给你个思路吧。设定一个字符数组,或者直接用string对象,从键盘接收一个字符串到该字符数组或字符串对象中。然后设一个变量i用以遍历字符串,如果遇到第i位是空格或者标点,则空格或标点数加1,并检查第i-1位是否为字母,如果i-1位是字母,说明刚刚遍历过去的是一个单词,则单词数加1,否则(i-1位不是字母)就继续往下走。
当然你还要设三个变量存储空格、标点和单词的数量。
期间还要注意一些问题,比如字符串首位是标点或者空格的问题等等。本回答被提问者采纳
第2个回答  2009-03-05
统计单词数可以借助空格数来完成,仍然用getchar()来做,只是这样要准确判断单词就必须严格输入
第3个回答  2009-03-05
描述一下思路:
利用cin.getline读入一行字符存储在一块缓冲区中,然后对这块区一个字符一个字符的判断的,对符合题目要求的字符用一个变量计数累加,直到遇到'\0'
第4个回答  2009-03-05
#include <stdio.h>
void main()
{

char a;
int x=0,n=0,s=0,q=0;//x是英文字母,n是空格,s是数字,q是其他..

printf("请任意输入一行字符:");
fflush(stdin);
do
{
a=getchar();
if(a<='z'&&a>='a'||a<='Z'&&a>='A') x++;
else if (a==' ') n++;
else if (a<='9'&&a>='0')s++;
else q++;
}while (a!='\n');

printf("\n您输入了%d个英文字母,%d个空格,%d个数字,%d个其他字符\n",x,n,s,q-1);
}
第5个回答  2009-03-05
按标准格式输入一行字符,即空格是单词的分界,标点是单词的分界(不要在标点后还加空格)。

#include<iostream.h>
int main()
{
char a,t;
int wd=0,sp=0,pc=0,temp=0;
cout<<"请输入一行任意字符:";
a=cin.get();
if (a =='\n')
{
cout<<"Word"<<", "<<"space bar"<<", "<<"punctuation"<<endl;
cout<<0<<", "<<0<<", "<<0<<endl;
return 0;
}
while (a == ' ')
{
sp++;
temp = sp;
a=cin.get();
}
while(a !='\n')
{
if (a==','||a=='.'||a==';'||a=='?'||a=='\''||a=='\"'||a=='!')
{
pc++;
}
else if (a ==' ')
{
sp++;
}
else if(a>='a'&&a<='z'||a>='A'&&a<='Z');
else other++;
t=a;
a=cin.get();
}
if (t!=','||t!='.'||t!=';'||t!='?'||t!='\''||t!='\"'||t!='!')
{
wd = sp+pc+1-temp;
}
else wd = sp+pc-temp;

cout<<"Word"<<", "<<"space bar"<<", "<<"punctuation"<<endl;
cout<<wd<<", "<<sp<<", "<<pc<<endl;

return 0;
}
第6个回答  2009-03-05
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int WordsCount(string &pText);

int CountWordsInText(string &pFileName)
{
ifstream ifile;
string text;
int words = 0;
ifile.open(pFileName.c_str());
if (ifile.is_open())
{
while (!ifile.eof())
{
getline(ifile, text);
words += WordsCount(text);
}
}
ifile.close();
return words;
}

int WordsCount(string &pText)
{
int words = 0;
string::size_type pos = 0;
pos = pText.find_first_of(' ', pos);
while (pos != pText.npos)
{
pos++;
words++;
pos = pText.find_first_of(' ', pos);
}
words++;
return words;
}

int main()
{
string file = "test.txt";
cout<<CountWordsInText(file)<<endl;
return 0;
}

将要测试的文本文件放在工程目录下即可。
相似回答