C++ Program to reverse a string without using library function


In this program, You will learn how to reverse a string without using library function in C++.


Enter a string: hello

after reverse: olleh


Example: How to reverse a string without using library function in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    char str[100], temp;
    int size = 0, i = 0;

    cout << "Enter a String :";
    cin.getline(str, 100);

    while (str[size] != '\0') {
        size++;
    }

    size = size - 1;
    while (i < size) {
        temp = str[i];
        str[i] = str[size];
        str[size] = temp;
        size--;
        i++;
    }
    cout << "\nAfter Reverse String :" << str;

    return 0;
}

Output:

Enter a String :John Smith
After Reverse String :htimS nhoJ