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

我想请说下,C语言编程:从键盘输入一个字符串,分别显示字符串的每个字符及其对应的ASCII码。
最新回答
沉鱼落雁

2024-11-24 07:57:41

#include <stdio.h>
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;
}
}
//无论多么长字符串均可以
念河山远

2024-11-24 07:43:13

先定义一个数组(尽量大如:a[10000])i,n=0;
用for循环输入字符串
for(i=0;;i++)
{ scanf("%c",&a[i]);n++;
if(a[i]==‘\0’) break;}
输出也可也用for或while。
for(i=0;i<n;n++)
printf(“%c\n”,a[i]);
for(i=0;i<n;n++)
printf("%d\n",a[i]);

我建议你多看看书!