+ 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)
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;
}
0
Thank you very much Julien Quentin it was very useful