0
How can i scan a string with multiple spaces?
Suppose, "sololearn is a good app for learning code"
5 odpowiedzi
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;
}
+ 1
getline(cin, string_s) ;
+ 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....
+ 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;
0
Jayakrishna🇮🇳 how can i traverse the string using for loop?