0% found this document useful (0 votes)
2 views4 pages

C++ Practical1 Program Reverse String (HSC-CS)

Uploaded by

omkarraskat8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

C++ Practical1 Program Reverse String (HSC-CS)

Uploaded by

omkarraskat8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program: Reverse a String Using Function in C++

Q1. Write a C++ program to enter a string and display its reverse using a user-
defined function.

Code:
#include <iostream>

#include <string>

using namespace std;

void reverseString(string str)

for(int i = [Link]() - 1; i >= 0; i--)

cout << str[i];

int main()

string str;

cout << "Enter a string: ";

getline(cin, str);

cout << "Reversed string: ";

reverseString(str);

return 0;

}
Code Explanation
1. void reverseString(string str)

(This function receives the string as an argument.)

2. for(int i = [Link]() - 1; i >= 0; i--)

( Starts from the last character of the string and moves backward.)

3. cout << str[i];

(Prints each character one by one in reverse order.)

4. reverseString(str);

(Function call from the main() function.)


Flowchart
Sample Output
Example 1
Input:

Enter a string: Computer

Output:

Reversed string: retupmoC

Example 2

Input:

Enter a string: Programming

Output:

Reversed string: gnimmargorP

Example 3

Input:

Enter a string: MADAM

Output:

Reversed string: MADAM

Note: MADAM is a palindrome because it remains the same when reversed.

You might also like