+ 3
How to remove all excessive whitespaces?
The code #include <iostream> using namespace std; int main(int argc, char *argv[]) { string s(" aaaaa mmmm "), s1; cout<<s<<","<<'\n'; int d=s.length(), d1; for(int i1=0; i1<d; i1++) { if((s[i1]==' ' && s[i1+1]==' ')) continue; if(i1==0 && s[i1]==' ') { s[i1]==s[i1+1]; d--; } else s1+=s[i1]; } cout<<","<<s1<<","<<endl; } We have to get string "aaaaa mmmm"
11 Respostas
+ 3
Create a new string .. then use a for loop on s to go through each character to see if it's a white space or a letter .. if it's a letter add the letter to the new string .. also check if a white space come after a letter .. add the whitespace also
But if a whitespace come first intro the string.. or a whitespace come after a whitespace ... Don't add it! That's one way to do it.. and the simplest I think ..
Edit:
i see people already started to post the whole code .. yeah good.. for people who want to copy paste.. without understanding how it's done.. don't copy it.... just use the theory .. and try to implement they yourself.. it's gone help you a lot.. and you will see if you really understand .. how to work with loops, conditions, and etc.. so if you wanna learn .. and understand how it's done.. use the theory.. and implement the solution yourself.
+ 3
You can also explore using the find, replace, substr methods of the string type.
https://cplusplus.com/reference/string/string/
+ 3
To remove excessive whitespaces from a string, you can iterate through the string and remove any consecutive whitespace characters. Here's the corrected code:
```cpp
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
string s(" aaaaa mmmm "), s1;
cout << s << "," << '\n';
int d = s.length();
for (int i1 = 0; i1 < d; i1++) {
if (s[i1] == ' ' && s[i1 + 1] == ' ')
continue;
if (i1 == 0 && s[i1] == ' ') {
s[i1] = s[i1 + 1]; // Corrected assignment operator
d--;
} else
s1 += s[i1];
}
cout << "," << s1 << "," << endl;
}
```
I corrected the assignment operator in the line `s[i1] == s[i1 + 1];` to `s[i1] = s[i1 + 1];` so it correctly removes the extra whitespace.
+ 3
TeaserCode since the input streaming operator >> skips spaces, you can use it to parse whole words without spaces.
Here is how: Make a stream from your input string. Then you can read from the stream and re-assemble the words into an output string with one space between them.
https://sololearn.com/compiler-playground/cHVAG7NCwJ9n/?ref=app
+ 1
Thank you very much for correct solution.
+ 1
Brian
simplest is the best.đ
+ 1
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
string s(" aaaaa mmmm "), s1;
cout << s << "," << '\n';
int d = s.length();
for (int i1 = 0; i1 < d; i1++) {
if (s[i1] == ' ' && (i1 == 0 || s[i1 - 1] == ' ')) {
continue;
}
s1 += s[i1];
}
cout << "," << s1 << "," << endl;
return 0;
}
You have just made a small mistakes
1.Symbol
2.iregular assigning of value.
If you encounter any problems just say the problem you're facing
0
To remove all excessive whitespaces from the string " aaaaa mmmm " and get the string "aaaaa mmmm", you can use the following code:
```cpp
#include <iostream>
using namespace std;
int main() {
string s(" aaaaa mmmm "), s1;
cout << s << "," << '\n';
bool isSpace = false;
for (char c : s) {
if (c == ' ') {
if (!isSpace) {
s1 += c;
isSpace = true;
}
} else {
s1 += c;
isSpace = false;
}
}
cout << "," << s1 << "," << endl;
return 0;
}
```
This code will remove excessive whitespaces from the input string and produce the desired output "aaaaa mmmm".
0
Sorry ,the last two solutions dont give desired string.
0
I need it for making phone dictionary code in one of coding problems. And now it is succesfuly solved.
0
Ok