C Program To Display Next And Previous Character of Given Character
This article is tutorial which explains how to write a C program which accepts a character from user and display the characters previous and next to it.
C Program to Display Next and Previous Character from given input Character
/* Aim: To display next and previous character */
#include<stdio.h>
int main()
{
char character;
printf("\n Enter any character:");
scanf(" %c",&character);
printf("\n The character next to '%c' is '%c'",character,character+1);
printf("\n \n The character previous to '%c' is '%c' \n \n",character,character-1);
return 0;
}
Output:
Enter any character: a The character next to 'a' is 'b' The character previous to 'a' is '`'
Explanation:
Accept a character from user and store it in the variable namedcharacter
. . Even though it is data type of our variable is char
it is internally treated in the form of ascii values. So when we perform character+1
or character-1
the ascii value of character is incremented or decremented by one. As we are using format specifiers %c in print we get the char
representation of ascii value. Try putting %d instead you will see the resultant ascii value. That's how we display character previous and next to given character.