C Program to Print Alphabet Position in Reverse Order

Introduction

In this post, I am going to write a c program to print alphabet position in reverse order. In other words, we will read a string and print that string in reverse order.

I am going to write two program for the above title. One is without using library function and another with standard library function i.e. strrev().

I have already written this type of program but using pointer only. In this post, I will write the same program without using pointer.

Also Read: Program To Reverse a String in C using Pointer

Before going further, let us see the expected output for this program.

Expected Output

c program to print alphabet position in reverse order

As you can see in the above output, I am asking the user to enter a string. Then I am displaying the original string as well as the reverse string.

Logic of this program is so simple. We know that string is nothing but a character array. So, I am simply printing that array in reverse order using while loop.

I don’t think you have any difficulty for understanding the logic. Now, let us see the actual program.

C Program to Print Alphabet Position in Reverse Order

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char str[100];
    int i=0;
    printf("Enter a string\n");
    gets(str);
    printf("Original String: %s",str);
    printf("\nReverse String: ");
    i = strlen(str)-1;
    while(i>=0)
    {
        putchar(str[i]);
        i--;
    }
    return 0;
}

Here, I am counting total characters in a string using strlen() function and subtracting 1 and storing that result in the variable i.

After that, I am running a while loop with condition that the value of i must be greater than or equal to zero. In the body, using putchar() function, displaying each and every character until this condition is true. For this, I am decrementing the value of i by 1.

When the value of i becomes negative, while loop will be terminated and we will get our output.

We can write the same program using standard library function strrev(). This function is defined inside the string.h header file.

Now, let us see this program also.

C Program to Print Alphabet Position in Reverse Order Using strrev() function

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char str[100];
    printf("Enter a string\n");
    gets(str);
    printf("Original String: %s",str);
    printf("\nReverse String: %s",strrev(str));
    return 0;
}

Instead of while loop, we got our output using single statement only. The output will be same for both these program.

I hope you have understood these two program. If you have any difficulty, please feel free to contact me.

Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

Leave a Comment