Codeforcoding

C++ program to create reverse of a string using loops

C++ program to create the reverse of a string using loops

In this tutorial, we will discuss the concept of C++ program to create the reverse of a string using loops

In this post, we are going to learn how to reverse every word of the given string by the user and display the reversed string as an output

 

here, we are used for loop, while loop and do-while loop for display reversed string of the given string

C++ program to create reverse of a string using loops
Reversed string

program to print the reverse of a string

Display reversed string using for loop

Program 1

The program request the user to enter a string and the program displays the reversed string of the given string using for loop in C++  language

#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;

int main()
{
    char str[100];//declare a character array
    int i,len,temp;
    cout<<"Enter a String as you wish\n";
    cin>>str; //input string
    len=strlen(str);
    for(i=0; i<len/2; i++){
        temp=str[i];
        str[i]=str[len-i-1];
        str[len-i-1]=temp;

    }
   cout<<"Given String is reversed here\n"<<str;
    getch();

    return 0;
}

When the above code is executed, it produces the following result

Program 1

Explanation

 

Display reversed string using while loop

Program 2

The program request the user to enter a string and the program displays the reversed string of the given string using while loop in C++ language

#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;

int main()
{
    char str[100];//declare a character array
    int i,len,temp;
    cout<<"Enter a String as you wish\n";
    cin>>str; //input string
    len=strlen(str);
    i=0;
    while(i<len/2){
        temp=str[i];
        str[i]=str[len-i-1];
        str[len-i-1]=temp;
 i++;
    }
   cout<<"Given String is reversed here\n"<<str;
    getch();

    return 0;
}

When the above code is executed, it produces the following result

Program 2

Explanation

 

Display reversed string using do-while loop

Program 3

The program request the user to enter a string and the program displays the reversed string of the given string using do-while loop in C++ language

#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;

int main()
{
    char str[100];//declare a character array
    int i,len,temp;
    cout<<"Enter a String as you wish\n";
    cin>>str; //input string
    len=strlen(str);
    i=0;
    do{
        temp=str[i];
        str[i]=str[len-i-1];
        str[len-i-1]=temp;
 i++;
    }while(i<len/2);
   cout<<"Given String is reversed here\n"<<str;
    getch();

    return 0;
}

When the above code is executed, it produces the following result

Program 3

 

Explanation

Suggested post

String manipulation in C language

String function in C language

For loop in C++ language

While loop in C++ language

Do-while loop in C++ language

 

Similar post

C++ program to display reversed string

C program to display reversed string

Java program to display reversed string

Display reverse of a string using loops in C language
Java code to reverse a string using loops
Exit mobile version