文字列から余分なスペースを削除するプログラムを作成しました。ただし、スペースの前の文字のみが表示されます。スペースを見つけて、その後、スペースかどうかをチェックします。余分なスペースに応じて、他の文字を余分なスペースに移動します。しかし、出力は非常に混乱します。
入力: "qwe(2スペース)rt(1スペース)y"
出力: "qwe(one space)rt(one space)y"
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main(){
string a;
cin >> a;
int len = a.length();
int new_len=len;
int z,s=0;
for(int i=0; i<new_len; i++){
if(a[i]==' '){
z=i+1;
s=0;
//Assigning the number of excess spaces to s.
while(a[z]==' '){
s++;
z++;
}
//doing the shifting here.
if(s>0){
for(int l=i+1; l<new_len-s; l++){
a[l]=a[s+l];
}
}
new_len-=s;
}
}
cout << a << endl;
cout << a.length();
system("pause");
return 0;
}