+ 1

How to solve this?

https://code.sololearn.com/cv6m4jPvD6bH/?ref=app so the code above suppose to print your name after you entered it (I know genius 😑), I want to print just the letters in ODD places from the entered name (ex:if you enter: Jhon , it will print: h , n). How can I do that?

2nd Dec 2017, 11:32 AM
Huda Zaher
Huda Zaher - avatar
4 Antworten
+ 5
#include <iostream> using namespace std; int main() { std::string name; cout << "Enter your name" << endl; cin >> name; int size = name.size() for(int i = 0; i < size; i++) { if(i % 2 != 0) { cout << name[i] << endl; } } }
2nd Dec 2017, 11:53 AM
Cool Codin
Cool Codin - avatar
+ 2
@Cool Codin I think you should use name.length() or name.size() instead of sizeof(name)/sizeof(name[0]); . It won't work as expected in all cases... After all, sizeof doesn't just return the size of the string times size of a char (1). https://drive.google.com/folderview?id=1psMK0Q-orarN6pRroX33YcxIMGQN3uak
2nd Dec 2017, 2:12 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
THX!
2nd Dec 2017, 12:04 PM
Huda Zaher
Huda Zaher - avatar
+ 1
thx, I'll try that.
2nd Dec 2017, 2:16 PM
Huda Zaher
Huda Zaher - avatar