0

A function to take a string and print its reverse

12th Oct 2016, 5:53 PM
Asim
Asim - avatar
3 Respostas
+ 4
this function will work just pass any string input to it. This function will print the reverse of input. void rev(char str[]) { int size=0; while(str[size]!='\0') size++; for(int i=size-1;i>=0;i--) cout<<str[i]; }
12th Oct 2016, 6:47 PM
Prabhakar Jha
Prabhakar Jha - avatar
0
void rev(string str) { for (int i = str.length() - 1; i >= 0; i--) cout << str.at(i); } Shorter 🙂
13th Oct 2016, 1:04 AM
Cohen Creber
Cohen Creber - avatar
- 1
std::string str; std::for_each(str.rbegin(), str.rend(), [](char c){cout<<c;}); shortest :) or void printRev(std::string str) { for (auto it = str.rbegin(); it != str.rend() && (cout << *it, true); ++it); }
13th Oct 2016, 9:33 AM
kiwiyou
kiwiyou - avatar