w3resource

C Exercises: Print characters in a reverse way

C Basic Declarations and Expressions: Exercise-4 with Solution

Reverse characters ('X', 'M', 'L')

Write a C program to print the following characters in reverse.

Pictorial Presentation:

C Programming: Print characters in a reverse way
Test Characters : 'X', 'M', 'L'

C Code:

#include <stdio.h>

int main() 
{
    // Declare and initialize character variables
    char char1 = 'X';
    char char2 = 'M';
    char char3 = 'L';

    // Print the original and reversed characters
    printf("The reverse of %c%c%c is %c%c%c\n",
        char1, char2, char3,
        char3, char2, char1);

    return(0);
}

Explanation:

In the exercise above -

#include <stdio.h> - This code includes the standard input/output library <stdio.h>.

  • In the "main()" function, it declares three character variables: 'char1', 'char2', and 'char3', and assigns them the values 'X', 'M', and 'L' respectively.
  • It uses the "printf()" function to display a formatted message. The message contains placeholders specified by %c, which represent characters.
  • Inside the "printf()" function, it provides the values to substitute for the placeholders. In this case, it provides 'char1', 'char2', and 'char3' followed by their reverse order: 'char3', 'char2', and 'char1'.
  • The program will print: "The reverse of XML is LMX" because it swaps characters in reverse order.
  • Finally, the main function returns 0 to indicate successful program execution.

Sample Output:

The reverse of XML is LMX

Flowchart:

C Programming Flowchart: Print characters in a reverse way

C Programming Code Editor:

Previous: Write a C program to print a big 'C'.
Next: Write a C program to compute the perimeter and area of a rectangle with a height of 7 inches. and width of 5 inches.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming-exercises/basic-declarations-and-expressions/c-programming-basic-exercises-4.php