Loop

Display reverse of a string using loops in C language

Display reverse of a string using loops in C language

In this tutorial, we will discuss the concept of Display reverse of a string using loops in C language

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

Reserved string

Display reverse of a string using loops

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 <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];//declare a character array
    int i,len,temp;
    printf("Enter a String as you wish\n");
    gets(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;

    }
    printf("Given String is reversed here\n%s ",str);
    getch();

    return 0;
}

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

Output 1

Explanation

  • A character array is declared the size which is equal to 100.
  • The variables i,len,temp are declared
  • The user is asked to enter a string and it is stored in the character variable of ‘str’
  • The length of the string is assigned to variable ‘len
  • The for loop is used to create the reversed string from the given string
  • The for loop is functioning until ‘len/2‘ is greater than ‘i’
  • Finally, the output is displayed as the reversed string

 

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 <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];//declare a character array
    int i,len,temp;
    printf("Enter a String as you wish\n");
    gets(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++;

    }
    printf("Given String is reversed here\n%s ",str);
    getch();

    return 0;
}

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

Output 2

Explanation

  • A character array is declared the size which is equal to 100.
  • The variables i,len,temp are declared
  • The user is asked to enter a string and it is stored in the character variable of ‘str’
  • The length of the string is assigned to variable ‘len
  • The while loop is used to create the reversed string from the given string
  • The while loop is functioning until ‘len/2‘ is greater than ‘i’
  • Finally, the output is displayed as the reversed string

 

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 <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];//declare a character array
    int i,len,temp;
    printf("Enter a String as you wish\n");
    gets(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);
    printf("Given String is reversed here\n%s ",str);
    getch();

    return 0;
}

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

Output 3

Explanation

  • A character array is declared the size which is equal to 100.
  • The variables i,len,temp are declared
  • The user is asked to enter a string and it is stored in the character variable of ‘str’
  • The length of the string is assigned to variable ‘len
  • The do-while loop is used to create the reversed string from the given string
  • The do-while loop is functioning until ‘len/2‘ is greater than ‘i’
  • Finally, the output is displayed as the reversed string

Suggested post

String manipulation in C language

String function in C language

input-output 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

 

 

Example program to check whether a Number is Prime or Not in C
C++ program to create reverse of a string using loops
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago