Reverse a string using iterator function

Reverse your friend's name

In this post we will be discuss the Program to reverse a string using iterator function i.e., we have to print the reverse of a string. Also, the required logic is discussed and the source code is written in C++ which is compiled successfully to provide the best results.

Reverse a string using iterator function

Problem Description

Your friend wants to see the pronunciation of his/ her name when it gets reversed. Help him/ her to find the reversed string using program.

Input (stdin)

Naveen Kumar

Output (stdout)

ramuK neevaN

Explaination :

In this we will be discussing the Program to reverse a string using iterator function i.e., we have to print the reverse of a string. This can be done by simple for-loop. We will input the string using getline function which will input aal the words in a given string and after applying the for loop with .rbegin and r.end function we can easily print the reverse part of the string.

👨‍💻Let’s write the required code for the problem :

Code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include<iostream>
#include<string>
int main() 
{ 
  std::string s;
  std::getline(std::cin,s);
  for(std::string::reverse_iterator
      i=s.rbegin();i<s.rend();i++)
  { 
      std::cout<<*i;
  }
  return 0;
}

Sample Test Cases have been passed successfully

If you liked this post, then share this with your friends and if anything is confusing or incorrect then let me know in the comment section.

Thanks from my side, this is Mayank, keep learning and exploring !!

 #Coding #Programming #string #learning

Next Post Previous Post
No Comment
Add Comment
comment url