gpt4 book ai didi

C语言输入字符串大小写修改代码

转载 作者:太空宇宙 更新时间:2023-11-03 23:59:19 25 4
gpt4 key购买 nike

#include <stdio.h>

int main()
{
int c;
while ( (c = getchar()) != EOF )
{
if (c >= 65 && c <= 90)
c += 32;
else if (c >= 97 &&c <= 122)
c -= 32;
putchar(c);
}
return 0;
}

在代码中如何解释、处理和打印输入?

例如,如果我们输入abcde,我们得到输出ABCDE。当我们逐个字符地处理输入时,我们应该逐个字符地获取输出,但是一旦我们按下回车键,我们就会得到输出。直到我们按下回车键,输出存储在哪里。

最佳答案

有哪些功能?

  • getchar() 是一个函数,用于从
    控制台。

putchar() is a function that is used to return the character written as an unsigned char cast to an int.

#include <stdio.h>
int main()
{
int c;
while ( (c=getchar()) != EOF ){
if (c >= 65 && c <= 90)
c += 32;
else if (c >= 97 &&c <= 122)
c -= 32;
putchar(c);
}
return 0;
}

c 是整数,因此当您从键盘读取字符时,它会将其存储为整数而不是字符。 checkout ASCII。

so 65 = A, 90 = Z, 97 = a, 122 = z,

这是我从代码中了解到的:

  1. create variable c to store our character.
  2. get character from user and store it to c.
  3. while c is not equal to EOF(end of file)
  4. if c is greater than or equal to 65 and c is less than or equal to 90
  5. add 32 to c
  6. or else if c is greater than 97 and c is less than or equal to 122
  7. minus 32 from c
  8. return c value into character and print it.

关于C语言输入字符串大小写修改代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51124690/

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