我正在用 C 语言编写一个程序,它打印字符串/文本的 ASCII 值包括空格。该程序工作正常,并给出包含空格的字符串的精确 ASCII 值,但问题是它还在所有 ASCII 值的末尾打印“10”。
这是我的代码:
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100];
int i;
printf("Enter a string: ");
fgets(str,100,stdin);
//scanf("%s",str);
printf("String is: %s\n",str);
printf("ASCII value in Decimal is: ");
for(i=0; str[i]!='\0'; i++)
{
printf("%d ",str[i]);
}
printf("\n");
getch();
}
请帮帮我,告诉我问题是什么以及如何解决。提前致谢。
最佳答案
当您使用 fgets
读取字符串时,您按下的输入将包含在字符串中 ( man page )。 \n
的 ASCII 代码为 10
。
关于c - C 程序中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43853749/