0

Why this code doesn't solve the #The Spy Life problem, while the output is the same as expected though?

#include <iostream> using namespace std; int main() { string input; getline(cin, input); string str1=""; for (int i=0;i<=input.size();i++) { if ((input[i] >= 65 && input[i] <= 90) || (input[i] >= 97 && input[i] <= 122) || input[i] == ' ') { str1 += input[i]; } } //cout << str1; // debug //string str2=""; for (int i=0;i<=str1.size();i++) { cout << str1[str1.size() - i]; } //if (str2 == "tacos") cout << "yes"; //cout << sizeof(str2)<<","<<sizeof("tacos"); //cout << "tacos"; return 0; } Edited minutes later: finally found that the size() method is returning the human-spoken size of the string. The size of "aaa" is actually 3 but not 2. So in my case i should write cout << str1[str1.size() - i - 1]; instead.

10th Mar 2022, 9:50 AM
Curahatos Wi
Curahatos Wi - avatar
2 Answers
+ 4
In second loop, i should start with 1 since, you're subtracting it with final output. int i = 1;
10th Mar 2022, 10:11 AM
Simba
Simba - avatar
+ 2
thanks guys
10th Mar 2022, 10:33 AM
Curahatos Wi
Curahatos Wi - avatar