C program to write String in reverse

Print the given string in reverse

Accept a string from user and print the given string in reverse
If a string is given as input then the output should be reverse of the given string
Example:
      If the given input is 'string' then the output should be reverse of the given input that is 'gnirts'

INPUT: string
OUTPUT: gnirts

Example:
      If the given input is 'codingPreparation' then the output should be reverse of the given input that is 'noitaraperPgnidoc'.

INPUT: codingPreparation
OUTPUT: noitaraperPgnidoc

Program

/*C program to write String in reverse */
#include<stdio.h>
#include<conio.h>
int main()
{
char s[50];
int i;
clrscr();
puts("Enter any string: ");
gets(s);
puts("\nThe string in reverse is: ");
for(i = 0; s[i] != '\0' ; i++);
for(i = i - 1; i >= 0; i--)
printf("%c ", s[i]);
return 0;
}

Output:

Enter any string: coding
The  string in reverse is: gnidoc

Program explanation:

stdio is used to connect the standard input and output. conio is used when we what to clear the screen by using some keyword or for some other purpose depending on the need of the user. #include will include the library's ,here stdio and conio are included. s[50] is an array of range 50 that is the string range is upto 50 characters. clrscr() is used to clear the screen . puts() is used to display the given text on the display screen i.e monitor. gets() is used to take the input that works same as scanf(). return() will return the value to the main()   


No comments:

Post a Comment