0

How can i scan a string with multiple spaces?

Suppose, "sololearn is a good app for learning code"

21st May 2020, 5:53 PM
Ashfaq Reshad
Ashfaq Reshad - avatar
5 Respuestas
0
Ashfaq Reshad string reverse(string str) { string temp = str; for (size_t i=0, j=str.length()-1; i<str.length(); i++, j--) { str[i] = temp [j]; } return str; }
21st May 2020, 7:02 PM
Mustafa K.
Mustafa K. - avatar
+ 1
getline(cin, string_s) ;
21st May 2020, 5:59 PM
Jayakrishna 🇮🇳
+ 1
how much you know,... tell me before that.. Any ways, it's same as you in c. String is like a Charecter array only with\0 at end. So string s="Hello World"; for(int i=0;s[i];i++) cout<<s[i]; This prints string s. You can use i<s.length() or s.size(), instead s[i] condition....
21st May 2020, 6:11 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 yes i agree, I think your first example is better. i < s.length() is not very good because the loop must scan the string over and over. its better to use size_t len = s.length() and i < len in a loop, or s[i] as you said. In C++ maybe a foreach loop is nicer for traversing strings. for (auto c : your_string) std::cout << c;
22nd May 2020, 12:02 AM
Gen2oo
Gen2oo - avatar
0
Jayakrishna🇮🇳 how can i traverse the string using for loop?
21st May 2020, 6:01 PM
Ashfaq Reshad
Ashfaq Reshad - avatar