w3resource

C Programming: Print individual characters of string in reverse order


4. Reverse String Characters

Write a program in C to print individual characters of a string in reverse order.

C Programming: Print individual characters of string in reverse order


Sample Solution:

C Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char str[100]; /* Declares a string of size 100 */
    int l, i; // Declare variables for string length and loop iteration

    printf("\n\nPrint individual characters of string in reverse order :\n"); // Display information about the task
    printf("------------------------------------------------------\n");
    printf("Input the string : ");

    // Read a string from the standard input (keyboard) using fgets()
    fgets(str, sizeof str, stdin);

    l = strlen(str); // Calculate the length of the string

    printf("The characters of the string in reverse are : \n");

    // Loop to print each individual character of the string in reverse order
    for (i = l - 1; i >= 0; i--) {
        printf("%c  ", str[i]); // Print each character in reverse order
    }

    printf("\n");
	
	return 0; // Return 0 to indicate successful execution of the program
}

The program can also be written as below:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char str[100]; /* Declares a string of size 100 */
    int l=0;
	
       printf("\n\nPrint individual characters of string in reverse order :\n");
       printf("------------------------------------------------------\n"); 	
       printf("Input the string : ");
       fgets(str, sizeof str, stdin);
	   l=strlen(str);
	   printf("The characters of the string in reverse are : \n");
       for(str[l]='\0';l>=0;l--)
        {
          printf("%c  ", str[l]);
        }
    printf("\n");
	
	return 0; // Return 0 to indicate successful execution of the program
}

Output:

Print individual characters of string in reverse order :                                                      
-----------------------------------------------------------                                                   
Input the string : w3resource.com                                                                             
The characters of the string in reverse are :                                                                 
                                                                                                              
  m  o  c  .  e  c  r  u  o  s  e  r  3  w 

Flowchart:

Flowchart: Print individual characters of string in reverse order


For more Practice: Solve these Related Problems:

  • Write a C program to print the characters of a string in reverse order using recursion.
  • Write a C program to reverse a string in place by swapping characters and then print the result.
  • Write a C program to display a string in reverse using pointer arithmetic without extra memory.
  • Write a C program to reverse a string’s characters while preserving the positions of whitespace.

Go to:


PREV : Separate String Characters.
NEXT : Count Words in String.

C Programming Code Editor:



Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.