Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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
The code has been copied automatically for you in the clipboard. After that open any of the service and press cntrl-v to paste in the given code area. Needs a bit of tweaking as per the link.
-OR-
Enter an alphabet :#
You did not enter an alphabet
-OR-
Enter an alphabet :Z
Next character is not an alphabet
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.