i just started learning c++ and i want to know how to print out the next character in alphabet when i input a character . i want the code to be in scanf and printf not cout and cin
[code]
i tried this code but its not working:
#include <stdio.h>
int main ()
{
char z;
scanf("%c",&z);
printf("%c",z+1);
return 0 ;
}
i want the code to be in scanf and printf not cout and cin
You are learning C. You are not learning C++. If you want to code in c++, use cout and cin.
Also. Your code works perfectly fine for me. It prints out the next character in the alphabet just like you want it to. What does it do for you?
Here is a similar program in C++.
1 2 3 4 5 6 7 8 9 10
#include <iostream>
usingnamespace std;
int main()
{
char a = 'a';
a++; // this is the same as doing a = a + 1;
std::cout << a; // prints out the letter b.
}
@TarikNeaj i know i should use cout and cin but i want to try it with printf and scanf .
for example when i put a character in the black screen then click enter it should give me the next letter in alphabet ( when i put the letter f then click enter it just gives me '!' )
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
constchar alphabet[] = "abcdefghijklmnopqrstuvwxyz" ;
char c ;
// note: conversion specifier %c does not consume leading whitespace
// unless there is a whitespace character before the %c in the format string
scanf( " %c", &c ) ;
c = tolower(c) ; // if upper case, convert c to lower case
if( c != 0 && c != 'z' ) // if not the null character and not the last character in our alphabet
{
constchar* ptr = strchr( alphabet, c ) ; // find the occurrence of c in our alphabet
if( ptr != NULL ) // if it is present in our alphabet, print out this character and
printf( "the next character after '%c' is '%c'\n", ptr[0], ptr[1] ) ; // the next character in our alphabet
}
}