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

Previous
Next

Solution:

  • We use fgets() function to read the string from the console.
  • stdin is the pre-defined object that represents keyboard standard input.
  • strlen() function is used to find the length of input string.
  • Using an iterator(for loop) we display individual characters or string in reverse order.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void main()
{
	char str[20]; 
	int l,i;
printf("***Print individual characters of string in reverse order***\n");
	printf("Enter input string : ");
	fgets(str, sizeof str, stdin);
	printf("The characters in reverse are : \n");
	l=strlen(str);
	for(i=l-1;i>=0;i--)
	{
		printf("%c ", str[i]);
	}
	printf("\n");
}
Previous
Next

Courses Enquiry Form