一道C++题目求助

2025-04-29 23:40:46
推荐回答(1个)
回答1:

#include

using namespace std;

int main()

{

char str[100];

str[0] = '\0';

char c = 0;

bool flag = true;

int n = 0;

while (c != '#')

{

c = getchar();

if (flag && (c == ' ' || c == '\n' || c == '\t' || c == '\n\r '))

continue;

else

flag = false;

if (n > 0)

{

if (c == ' ' || c == '\n' || c == '\t' || c == '\n\r ')

{

if (str[n - 1] == ' ')

continue;

else

{

str[n] = ' '; n++; continue;

}

}

}


str[n] = c;

n++;

}

str[n] = '\0';

if (n>1 &&str[n - 2] == ' ')

{

str[n - 2] = '#'; str[n - 1] = '\0';

}


cout << str;

}