返回前后字母的程序

A program that returns the letter that comes after and before

本文关键字:程序 返回      更新时间:2023-10-16

我是编程新手。任务是制作一个程序,返回给定字母的pred和succ字母作为输出数据。输入数据是 b 和 z 之间的任何字母。我已经声明了每个字母 b-z 作为自身的变量,并将输入数据声明为字母。但是我该怎么做呢?我能想到的一种方法是将字母定义为彼此的前身/继任者(对于每个字母(。但在我看来,这将需要比必要的更多的代码。

#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);
}

字母的ASCII表示是连续的。所以你能做的是

  • 获取输入
  • 将 1 加到它以获得继任者
  • 减去
  • 1 得到前置任务
  • 打印它们。

#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;
}