0

c++ deleting spaces

Welcome dear fellow members. I am struggling with some sphere online judge tasks and I would be very grateful if somebody could help me understand how the word splitting code works and how to implement some functionalities such as saving each character to separate cell in a table. The code: // C++ program to print words in a sentence #include <bits/stdc++.h> using namespace std; void removeDupWord(string str) { string word = ""; for (auto x : str) { if (x == ' ') { cout << word << endl; word = ""; } else { word = word + x; } } cout << word << endl; } // Driver function int main() { string str = "1 23 4 5"; removeDupWord(str); return 0; } This program works fine and shows the numbers in new lines. But is it possible to cout each cell of the string "word" ? for example cout << word[i] << endl; ? And why the first for loop uses "auto: x" ? Thank You in advance

20th May 2019, 12:00 PM
Mateusz Banaszak
Mateusz Banaszak - avatar
12 Respostas
+ 3
You can access single chars by using string.at(position) or string[position]. auto means the compiler will choose the correct data type for a variable automatically and the : syntax means that this loop will iterate over str and x will represent the single characters that occur. The loop is basically just a short version of char x; for (int i = 0; i < str.length; x = str[i++]) { ....
20th May 2019, 5:40 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 3
This does work for me: string word = "test"; cout << word[0]; Have you assigned word to a value?
20th May 2019, 7:08 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
I edited the code so it uses a vector to store the words. cout << words[0]; will print the first word: https://code.sololearn.com/cC0l0e805cXt/?ref=app
21st May 2019, 9:13 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
I'm sorry, I forgot a small detail, but this loop should be a exact replacement for "for (auto x : str) {": char x; for (int i = 0; i < str.length(); x = str[i++]) { But what exactly do you mean with last element of the table?
21st May 2019, 1:08 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
I made two small changes and commented them. Now it should work. https://code.sololearn.com/c58HVE798Gw7/?ref=app
21st May 2019, 1:24 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
You need to initialize the char with the value of the first letter of the string. You can replace the loop with following code: char x = str[0]; for (int i=0; i<str.length(); x=str[++i])
29th May 2019, 3:00 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
The "<=" conditional ! That was it :) I thing spoj will not add spaces after the last character but it's very nice to have that lines just in case. I would like to thank You very much for your help and the reviews, from now on I think I can handle those quests :)
21st May 2019, 1:37 PM
Mateusz Banaszak
Mateusz Banaszak - avatar
0
Thank You for the response - I am aware that single chars can be accessed same way it is done with tables - but wich of the variables is it ? I cannot do that using string[position] (for example - word[0]) the compiler shows nothing - I think I may did not understand it properly
20th May 2019, 5:59 PM
Mateusz Banaszak
Mateusz Banaszak - avatar
0
Yes, It does work for me string word = "test"; cout << word[0]; output: t But the word doesn't have spaces so it is not neccesary to use a word splitting program :) I would like this to work when i type from keyboard with spaces: "1 23 45 5"; cout<< word[0]; output: 1; cout<< word[1]; output: 23; cout<< word[2]; output: 45; cout<< word[3]; output: 5;
21st May 2019, 8:43 AM
Mateusz Banaszak
Mateusz Banaszak - avatar
0
https://code.sololearn.com/cC0l0e805cXt The code You wrote is a bit complicated for me since I must invest some time to learn and understand vectors and all of the methods You used. I wrote something simple and it is sufficient for now. My only question is why the compilator doesn't show the last element of the table ? And how to simplify the first for loop - for (int i = 0; i < str.length; x = str[i++]) doesn't want to compile
21st May 2019, 12:32 PM
Mateusz Banaszak
Mateusz Banaszak - avatar
0
I also forgot about that detail - but Your thinking was right. https://code.sololearn.com/cmvJmtG668ys - there are 4 elements meant to be saved to proper table cells, but the compiler only puts 3 first numbers from the specified string
21st May 2019, 1:12 PM
Mateusz Banaszak
Mateusz Banaszak - avatar
0
I have another issue according to the code that was written, Aaron - what if the string str has space at the beggining ? For example: " 1 2 23 4" The first table content will be saved as " 1" not "1". I am struggling to overcome that
29th May 2019, 12:52 PM
Mateusz Banaszak
Mateusz Banaszak - avatar