【发布时间】:2022-01-03 09:58:29
【问题描述】:
我想制作一个程序,将我的字符串输入(char)转换为 ASCII 数字,然后分离 ASCII 数字并将其转换为二维数组,然后像这样在输出中显示二维数组。
输入:有趣 (str 是我用于输入的字符串的名称)
//The string char input
str = fun
然后程序将输入(字符串 char)转换为 ASCII 数字。 (ascii 是我用于 ASCII 数字的数组/字符串的名称)
//ASCII numbers for 'fun' (f u n)(left to right)
ascii = 102 117 110
注意:102 是 ASCII 中的字母“f”,117 是 ASCII 中的字母“u”,110 是 ASCII 中的字母“n”。
然后,程序分离 ASCII 数字并将其转换为二维数组。 ('asc' 是二维数组的名称)
//ASCII for letter 'f'
asc[0][0] = 1
asc[0][1] = 0
asc[0][2] = 2
//ASCII for letter 'u'
asc[1][0] = 1
asc[1][1] = 1
asc[1][2] = 7
//ASCII for letter 'n'
asc[2][0] = 1
asc[2][1] = 1
asc[2][2] = 0
输出:1-0-2--1-1-7--1-1-0
这是该程序的输入和预期输出:
Input: fun
Output: 1-0-2--1-1-7--1-1-0
这是我制作的完整代码:
#include <stdio.h>
#include <string.h>
int main(){
char str[50];
printf("Input: ");
scanf("%s", &str); //string input
int i;
int ascii[50];
for(i = 0; i < strlen(str); i++){
ascii[i] = str[i]; //converting string to ascii
}
int x, y;
int asc[50][3];
for(x = 0; x < strlen(str); x++){
for(y = 0; y = strlen(str); y++){
asc[x][y] = ascii[x]; //separate ascii to 2d array
}
}
printf("Output: ");
for(x = 0; x < strlen(str); x++){
for(y = 0; y < strlen(str); y++){
printf("%d", asc[x][y]); //showing the result of separation/conversion
printf("-");
}
}
return 0;
}
你能告诉我代码有什么问题吗?它不输出想要/预期的结果。相反,输入永远循环(输入不会停止)。提前感谢您为我提供解决此问题的方法!
(注意:程序仅限于只能使用两个头文件(stdio.h和string.h)。程序也仅限于只能使用三个str函数(strcpy,strcmp,strlen)(多少它在程序中是无限的。)。我也不能使用gets、puts、define等。)
【问题讨论】:
-
“输入永远循环”和“输入不会停止”究竟是什么意思?请给出准确的运行日志。
-
注意:这个程序我只能使用基本功能
标签: c for-loop multidimensional-array ascii c-strings