Answer the question
In order to leave comments, you need to log in
Yandex.Practice C++ What am I doing wrong?
Hey! What am I doing wrong?
Quest(link)
green parrot
5
6
13
#include <iostream>
#include <string>
using namespace std;
int main() {
string query;
getline(cin, query);
// выведите все индексы символов, следующие за словами
for (int i = 0; i < query.size(); i++){
if(to_string(query[i]) == to_string(32)){
cout << i << endl;
}
}
}
Answer the question
In order to leave comments, you need to log in
The whole check should be query[i] == ' '
to_string(32)
- will return you "32"
instead of a space. Then you can do (char)32
or static_cast<char>(32)
. But ' '
it's still better.
Also, you don't print the length of the entire string at the end - after all, a word always ends there (may be empty).
Why to_string? The elements of a string are characters. In order to compare them, all of a sudden, you need to compare two characters.
Found the answer!
At the end of the main function it was necessary to add:
Code:cour << query.size() << endl;
#include <iostream>
#include <string>
using namespace std;
int main() {
string query;
getline(cin, query);
// выведите все индексы символов, следующие за словами
for (int i = 0; i < query.size(); i++){
if(to_string(query[i]) == to_string(32)){
cout << i << endl;
}
}
cout << query.size() << endl;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question