gpt4 book ai didi

用 C 语言编写一个字母表,显示前面和后面的字母

转载 作者:行者123 更新时间:2023-11-30 16:58:29 28 4
gpt4 key购买 nike

所以我需要在 C 中发生这种情况,考虑了一个小时左右,但不知道该怎么做,或者如何向谷歌询问这个特定的东西,它必须与输入的任何字母一起工作。

如何计算给定字母的字母表中的下一个和上一个字母?

这是一个链接: http://imgur.com/bRdLonZ

enter image description here

最佳答案

虽然 Dac Saunders 已经给出了很好的解释和一个很好的草图,但我认为有第二个稍微不同的示例也可能会有所帮助。

// for printf() and puts()
#include <stdio.h>
// for exit() and the macro EXIT_SUCCESS
#include <stdlib.h>
// for tolower() and isupper()
#include <ctype.h>

// All calculations assume ASCII encoding!

int main()
{
char letter;

puts("Alphabet Facts!");
puts("^^^^^^^^^^^^^^^");

// You want an upper case letter as the input, so check for it
do {
printf("\nPlease input an uppercase letter: ");
letter = getc(stdin);
// swallow new-line(s)
getc(stdin);
#ifdef _WIN32
getc(stdin);
#endif
} while (!isupper(letter));

printf("\nSome interesting facts:\n\n");

printf(" 1) The lowercase version of the letter is '%c'.\n\n", tolower(letter));
if (letter == 'A') {
// or print 'Z' if you want it to mimic a circle
printf(" 2) No letter comes before '%c' in the alphabet.\n", letter);
} else {
printf(" 2) The letter '%c' comes before '%c' in the alphabet.\n\n", letter - 1, letter);
}
if (letter == 'Z') {
// or print 'A' if you want it to mimic a circle
printf(" 3) No letter comes after '%c' in the alphabet.\n\n", letter);
} else {
printf(" 3) The letter '%c' comes after '%c' in the alphabet.\n\n", letter + 1, letter);
}
// 'A' is at position 64 in the table of the ASCII encoding but we don't need to
// look it up, we can use the letter itself because a 'char' is a number, too.
// The ASCII table is zero based--we need to subtract unity from 'A' or add unity
// after the subtraction of 'A'.
printf(" 4) %c is letter number %d in the alphabet!\n\n", letter, letter - ('A' - 1));
// return the special number to the OS that tells it that no error occured.
exit(EXIT_SUCCESS);
}

关于用 C 语言编写一个字母表,显示前面和后面的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38799692/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com