如何使用 getche() 计算短语的单词并忽略 C++ 中带有两个字符的单词

how to count a words of a phrase and ignore the words with two characters in c++ using getche()

本文关键字:单词 字符 两个 C++ getche 何使用 计算 短语      更新时间:2023-10-16

我是 C++ 的新手,我在大学里有一个程序要求计算大小大于两个字符的单词和用户输入的短语中所有字符(忽略空格)的数量。程序使用头文件中的函数 getche() 逐个字符接受短语,直到按 Enter 键。与程序的交互可能如下所示:

输入文本:欢迎使用 C++ 字数为:2 字符数为:我停止了这一步,发现忽略两个字符的单词有问题,请帮忙...

#include <iostream>
#include<conio.h>
using namespace std;
int main(){
int chcount=0;
int wdcount=1;
int spccount=0;
char ch;
cout<<"Please,Enter a phrase :"<<endl;

while((ch=getche())!='r'){
if(ch!=' '){
    chcount++;
}else if (ch==' '){
    wdcount++;

}

}
cout<<"Count of words is : "<<wdcount<<endl;
cout<<"Count of Letters is : "<<chcount<<endl;
//cout<<"Count of space :"<<spccount<<endl;
return 0;
}
#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
int chcount = 0, wdcount =0, count = 0;
char ch='a';
cout << "Enter your text : ";
while ( ch != 'r' )
{
  ch = getche();
  if ( ch !=' ' )
  {
       chcount++;
       count++;          
  }
  else if (count > 2)
       {
                wdcount++;
                count=0;
       }
}

cout<<"nCount of words is: "<<wdcount+1<<"nCount of charcters is: "<<chcount-1<<"n";
system("pause");
return 0;}

顺便说一下,我是你的同事卡里姆·福阿德:)希望这有帮助