【发布时间】:2019-11-11 21:56:09
【问题描述】:
我正在创建一个程序,允许用户输入单词的长度,并返回与该长度对应的单词数。
这方面我已经完成了
我的问题如下:我应该有一个子进程来计算并返回与长度对应的单词,然后再次要求用户输入另一个长度。但是,如果某个长度的字符串为0,我应该让子进程无限期地重做计数,只通过单独的终端终止而不终止父进程。
感谢任何帮助。
我的代码如下
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
int WordsCount(char *string,int len){
int count=0,Wordcount=0;
while(*string){
//Checks for Delimeter
if (*string== ' ' || *string== ',' || *string== '\t' || *string== '\n' || *string== '.' || *string== '!' || *string == '\0'){
if (count==len){ // Checks If calculated length equals to User given length
Wordcount++;
count=0;
string++;
}
else{
count=0;
}
}
else{
count++;
}
string++;
}
return Wordcount; //Returns The Final Count of Words
}
int main()
{
const char string[] = "Maids table how learn drift but purse stand yet set. Music me house could among oh as their."
"Piqued our sister shy nature almost his wicket. Hand dear so we hour to. He we be hastily offence effects he service."
"Sympathize it projection ye insipidity celebrated my pianoforte indulgence. Point his truth put style."
"Elegance exercise as laughing proposal mistaken if. We up precaution an it solicitude acceptance invitation."
"Acceptance middletons me if discretion boisterous travelling an. She prosperous continuing entreaties companions unreserved you boisterous."
"Middleton sportsmen sir now cordially ask additions for. You ten occasional saw everything but conviction. Daughter"
"returned quitting few are day advanced branched. Do enjoyment defective objection or we if favourite. At wonder afford so danger cannot former seeing."
"Power visit charm money add heard new other put. Attended no indulged marriage is to judgment offering landlord. Parish so enable innate in formed missed."
"Hand two was eat busy fail. Stand smart grave would in so. Be acceptance at precaution astonished excellence thoroughly is"
"entreaties. Who decisively attachment has dispatched. Fruit defer in party me built under first. Forbade him but savings"
"sending ham general. So play do in near park that pain.";
int Length;
printf("Enter Word Length?\n");
scanf("%d",&Length); //Reading Length of the word from User
int result = WordsCount(string,Length); // Return Count Storing.
printf("Count = %d\n",result);
return 0;
}
【问题讨论】:
-
您应该避免使用变量名称,例如
string。如果您切换到 C++ 编译器,它可能会给您带来问题。 -
欢迎您。很高兴我能帮上忙。