Signup/Sign In

C++ Program to Reverse a String without using System Defined method

Hello Everyone!

In this tutorial, we will learn how to demonstrate how to find the reverse of the String without using the System Defined method, in the C++ programming language.

Steps to find Reverse of a String:

  1. Take the String to be reversed as input.

  2. Initialize another array of characters of the same length to store the reverse of the string.

  3. Traverse the input string from its end to the beginning and keep storing each character in the newly created array of char.

Code:

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to Reverse the String without using System defined function, in CPP  ===== \n\n";

    //Variable Declaration
    char s1[100], c = 'a';
    int n = 0, i = 0;

    cout << "\n\nEnter the String you want to reverse: ";
    cin >> s1;

    //Computing string length without using system defined method
    while (c != '\0')
    {
        c = s1[i];
        i++;
    }
    n = i - 1;

    cout << "\n\nLength of the entered string \"" << s1 << "\" is : " << n << "\n\n\n";

    //Declaring another char array to store the reverse of the string
    char s2[i];

    i = 0;
    //Logic to store the reverse of a string in another char array
    while (i != n + 1)
    {
        s2[i] = s1[n - i - 1];
        i++;
    }

    cout << "\n\nReverse of the entered string \"" << s1 << "\" is : \"" << s2 << "\"\n\n\n";

    return 0;
}

Output:

C++ string reverse

We hope that this post helped you develop a better understanding of the concept of finding the reverse of the string without using the system-defined method in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.