我无法让我的程序删除在句子中找到的额外空格,例如空格后还有另一个空格。而我用来要求用户继续的 while 循环正在阻止函数运行。当我删除它时,程序的其余部分就会运行。
这是主要功能:
int main()
{
bool program_start = 1;
char user_sentence[MAX];
cout<<"This program will take any sentence you write and correct any spacing\n"
<<" or capitalization (not proper nouns) mistakes you have.\n";
while(loop_continue(program_start))
{
input(user_sentence, MAX);
uppercase_spacing(user_sentence);
lowercase(user_sentence);
cout<<user_sentence;
}
return 0;
}
我的问题是我的 uppercase_spacing 函数,我无法让它删除句子中的多余空格。
以下是我用来编辑句子的 void 函数:
void uppercase_spacing(char sentence[])
{
int number = 0;
if(isspace(sentence[0]))
{
for(int i = 0; i < MAX; i++)
{
sentence[i] = sentence[i + 1];
}
while(number<MAX)
{
if((isspace(sentence[number])) && (isspace(sentence[number+1])))
{
sentence[number] = sentence[number + 1];
}
number++;
}
sentence[0] = toupper(sentence[0]);
}else{
for(int index = 0; index<MAX;index++)
{
if((isspace(sentence[index])) && (isspace(sentence[index+1])))
sentence[index]=sentence[index+1];
}
sentence[0] = toupper(sentence[0]);
}
return;
}
void lowercase(char sentence[])
{
for(int i = 1; (i < MAX) && (i != '\0'); i++)
{
sentence[i] = tolower(sentence[i]);
}
return;
}
我在所有其他程序中都使用了这个布尔值,所以我相信问题出在程序的主要部分。
这是我用于 while 循环的布尔函数:
bool loop_continue(bool another_round)
{
bool again = another_round;
char continue_loop = 'y';
cout<<"Would you like to continue? Type y or Y for yes,\n"
>>" or any other letter for no.\n";
cin>> continue_loop;
if((continue_loop == 'y' )||(continue_loop == 'Y'))
{
cout<<"Okay, let's do this!"<<endl;
again = 1;
}else
{
cout<<"Goodbye."<<endl;
again = 0;
}
return again;
}
输入;引号代表空格。:
糖果CCandy""""什么的
输出; 空间仍然存在:
糖果ccandy""""什么的