-3

Im new to programming. The mission is to make a program that returns the pred and succ letter of a given letter as output data. The input data is any letter between b and z. I have declared every letter b-z as variables of themselves, and the input data as letter. But how do I proceed? One way that I can think of is defining the letters as predecessors/successors of one another (for each letter). But that would take more code than necessary, it seems to me.

#include <stdio.h>

int main(void)
{

    char b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,k,r,s,t,u,v,z;

    char letter;

    printf("Type a letter between b and z> ");

    scanf("%c", &letter);

}
Swifty
  • 839
  • 2
  • 15
  • 40

1 Answers1

2

ASCII representations of the letters are consecutive. So what you can do is

  • get input
  • add 1 to it to get the successor
  • subtract 1 to it to get predecessor
  • print them.

#include<stdio.h>
int main(){
    char c;
    scanf("%c",&c);
    if( c<='z' && c>='b')
        printf("succ  = %c pred = %c", c-1, c+1);  
    else
        printf(" You didnt enter between a and z"); 
    return 0;
}
user2736738
  • 30,591
  • 5
  • 42
  • 56