C语言编程:从键盘输入一个字符串,分别显示字符串的每个字符及其对应的ASCII码。

C语言编程:从键盘输入一个字符串,分别显示字符串的每个字符及其对应的ASCII码。,第1张

#include <stdioh>
struct text
{
char c;
struct text next;
};
void main()
{
text head = new text;
text current = head;
text next = NULL;
char c;
while(1)
{
if((current->c = getchar()) == '\n')
break;
//printf("%c:%d\n", current->c, current->c);
next = new text;
current->next = next;
current = current->next;
}
current->next = NULL;
current = head;
while(current->c != '\n')
{
printf("%c:%2x\n", current->c, current->c);
current = current->next;
}
}
//无论多么长字符均可以

有很多方法:
1、可以用sacnf或gets函数直接接收输入的字符串,然后用循环遍历字符串中每一个字符,判断其是字母还是数字。
2、可以用循环调用getch()或getchar()函数来一个一个字符的接收输入,同时判读输入的是字母还是数字。
至于怎么判读是字母还是数字,很简单,假设字符c,
满足 (c>='a'&& c<='z')|| (c>='A' && c<='Z')就是字母。
满足 (c>='0' && c<='9')就是数字。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12848487.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-28
下一篇 2023-05-28

发表评论

登录后才能评论

评论列表(0条)

保存