- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
C Program for Printing Next Successive and Adjacent Characters
In this tutorial, we will learn how to create a program in C that will print the next successive character and an adjacent character. First, we have created a program that will ask the user to enter any character to find and print its next successive character. Also, we have created a program that will take a character as input (from the user at run-time) and then print its adjacent character.
C Print the Next Character
Let's first create a program that will print the next successive character of a given character entered by the user. Let's suppose that if the user has entered a as a character input, then the program will print b (the next successive character of a). Or, if the user types in the letter C, the program will print out the letter D.
The question is: write a program in C to accept a character from the user and process it in these two ways:
- If the character is an alphabet, print the character after it. For example, if the user enters a, then print b. If x, then print y, and if z, then print a.
- If it is not an alphabet, then print the character itself as output.
The answer to this question is given below:
#include<stdio.h> #include<conio.h> int main() { char ch; printf("Enter any character: "); scanf("%c", &ch); printf("\n"); if(ch>=65 && ch<90) printf("%c", ch+1); else if(ch>=97 && ch<122) printf("%c", ch+1); else if(ch==90) printf("%c", 65); else if(ch==122) printf("%c", 122); else printf("%c", ch); getch(); return 0; }
As the program given above was written in the Code::Blocks IDE, here is the sample run after a successful build and run:
Now supply any character as input, say "a," and press the ENTER key to see the next character that comes after "a." Here is the second snapshot of the sample run:
Let's run another sample of the same program. This time, provide the input as Z and press the ENTER key:
Here is another sample run that checks what will happen if the user supplies any input that is not an alphabet character:
Program Explained
- Receive any character as input.
- A-Z's ASCII code is 65-90. That works out to 65 for A, 66 for B, 67 for C, and 90 for Z.
- The ASCII code for letters a-z is 97-122. 97 for a, 98 for b, 99 for c,... 122 for z.
- Now check whether the character's ASCII code is greater than or equal to 65 and less than 90 or not.
- If it is, then increment the value of the character and print it out as the next successive character.
- Otherwise, check whether the character's ASCII code is greater than or equal to 97 and less than 122 or not.
- If it is, then again increment and print the value of the character as output.
- Here we have excluded the ASCII code for Z (capital) and z (small), because if the user enters z, then we have to print a as its successive character, and if the user enters Z, then we have to print A as its successive character, therefore we have checked using the ASCII code and printed the value accordingly.
- Finally, if none of the preceding conditions evaluate to true, print the character as is. That is, if the given character does not fall under a-z or A-Z, print the character as it is on the output screen.
Print the adjacent characters of a given character in C
Let's create another program that will print the adjacent character of any given character entered by the user at run-time. In this case, adjacent characters are the two characters that come before and after the given character. For example, if the user has entered A, then print its adjacent characters as Z and B. Or if the user has entered D, then print C and E.
#include<stdio.h> #include<conio.h> int main() { char ch; printf("Enter any character: "); scanf("%c", &ch); printf("\n"); if(ch>65 && ch<90) printf("Adjacent characters = %c and %c", ch-1, ch+1); else if(ch>97 && ch<122) printf("Adjacent characters = %c and %c", ch-1, ch+1); else if(ch==90) printf("Adjacent characters = %c and %c", ch-1, 65); else if(ch==122) printf("Adjacent characters = %c and %c", ch-1, 97); else if(ch==65) printf("Adjacent characters = %c and %c", 90, ch+1); else if(ch==97) printf("Adjacent characters = %c and %c", 122, ch+1); else printf("%c", ch); getch(); return 0; }
Here is the final snapshot of the sample run:
The concept used in the preceding program is similar to that used in the preceding program. Except that we have to print the previous character along with the next character. Therefore, we have used ASCII code for A, a, Z, and z in separate cases.
« Previous Program Next Program »