0
A function to take a string and print its reverse
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];
}
0
void rev(string str)
{
for (int i = str.length() - 1; i >= 0; i--)
cout << str.at(i);
}
Shorter 🙂
- 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);
}