输入字符串a,要去除a内部的空格,得到新的a,然后对a做别的操作……
越短越好
C++用库<string>函数 s.push_back(); 来遍历添加字符的:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string t,s;
getline(cin, t);
for(size_t i=0;i<t.size();i++)
if (t[i]!=' ') s.push_back(t[i]);
cout << s << endl;
return 0;
}
求求了,很重要,我找到的办法都行不通
//假设以读取完毕,处理如下
string ss = "123 456 789 arfg af\nfgad adfg",res,tmp;
for(char c: ss){
if(c!=' ')res += c;
}
cout << "1." << res << endl;
res.clear();
int nn = 255;//大小可自己设定
char buf[nn];
cout << "please input:";
cin.getline(buf,nn);//只能获取一行输入,无法换行输入
for(char c: buf){
if(c == '\0') break;
if(c!=' ')res += c;
}
cout << "2." << res << endl;
res.clear();
while(cin >> tmp)res += tmp;//ctrl+c退出循环、无法换行符会被略过
cout << "3." << res << endl;
void func(char* str) {
int i = 0, j = 0;
int len = strlen(str);
while (j < len) {
if (str[j] != ' ') {
str[i] = str[j];
i++;
j++;
}
else {
j++;
}
}
str[i] = '\0';
}