清除多余的空格

来源:互联网 发布:淘宝店铺的网址怎么看 编辑:程序博客网 时间:2024/05/01 09:35
#include<iostream>using namespace std; int main(){char str[80] = "we     are    student.";cout << str << endl;int index = 0;bool flag = false;for (int i = 0; str[i] != '\0'; i++){if (str[i] != ' '){str[index++] = str[i];flag = true;}else if (flag){str[index++] = str[i];flag = false;}}if (index > 0 && str[index - 1] == ' '){str[index - 1] = '\0';}else{str[index] = '\0';}cout << str << endl;return 0;}

0 0