+ 1

How to print olny 2 letters from start and 2 letters from end of a string for example from Hello world i want only Held in outpu

#include <iostream> #include <cstring> #include <string> #include <list> using namespace std; int main() { string a ; string f; string b; getline(cin,a); if(a.length()==2){ a.append(a); cout<<a<<endl; } else if (a.length()<2){ cout<<"error"<<endl; } else if (a.length()>2){ f =a.front(); b =a.back(); cout<<f<<b<<endl; } return 0; } in this example it prints only (Hd) but i want it print (Held)

1st Dec 2018, 7:35 PM
Wahid Rohin
Wahid Rohin - avatar
2 Answers
+ 1
You can think of string like an array. int main(){ int size; string a; getline(cin,a); size = a.size(); size--; cout << a[0] << a[1] << a[size] << a[size--] << endl; return 0; }
9th Apr 2019, 12:40 AM
Cheng Xiong
Cheng Xiong - avatar
0
Thank you very much Julien Quentin it was very useful
1st Dec 2018, 7:51 PM
Wahid Rohin
Wahid Rohin - avatar