Print next alphabet if possible – CSKC
Sign In

Search

Solved Problem #CPP#2157    siteicon   siteicon  

Problem Statement - Print next alphabet if possible

Write a c++ program to collect an alphabet from the user and then print the next alphabet using the getchar(),  putchar() and isalpha() functions. Give suitable error message if next character is not an alphabet or the given entry by the user is not an alphabet.

Solution

TC++ #2157

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

c=getchar(); 
User input is collected in c using getchar().

if(isalpha(c))
Checking for alphabet range is done here

if(isalpha(c+1))
If it is alpha then we check if next character is also alpha. With c+1 we can check for next character easily. For this kind of check character is automatically converted by compiler to ASCII number added by 1 and back to character.

cout<<“Next alphabet is “; putchar(c+1);
Next alphabet is printed here. We could have printed c+1 in cout chain, but we wanted to use putchar() function.

else cout<<“Next character is not an alphabet”;
This comes into action inside the inner if when next character is not alpha.

else cout<<“You did not enter an alphabet”;
This comes into action with the outer if when the user entry is not an alphabet.

Notes

  • If we use c+1 in the cout chain we have to type cast it to ((char) c+1) as without casting it will print the ascii code. In case of putchar(c+1), typecasting is not required as the variable type in the putchar parameter is assumed to be char data type.


Share

Created: 4-Jan-2018 01:16 | Updated: 4-Jan-2018 01:24|