[英]Converting a char string into ASCII numbers and then separate the ASCII numbers & convert it into a 2d array using C language
我想制作一个程序,将我的字符串输入(char)转换为 ASCII 数字,然后将 ASCII 数字分开并将其转换为二维数组,然后像这样在 output 中显示二维数组。
输入:fun(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
Output:1-0-2--1-1-7--1-1-0
这是该程序的输入和预期 output:
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;
}
你能告诉我代码有什么问题吗? 它不是 output 想要/预期的结果。 相反,输入永远循环(输入不会停止)。 预先感谢您为我提供解决此问题的方法!
(注:程序仅限于只能使用两个header(stdio.h和string.h)。程序也仅限于只能使用三个str function(strcpy,strcmp,strlen)(多少在程序是无限的。)。我也不能使用gets,puts,define等。)
认为这部分代码会做什么:
for (x = 0; x < strlen(str); x++) {
for (y = 0; y = strlen(str); y++) {
asc[x][y] = ascii[x]; //separate ascii to 2d array
}
}
首先,你应该注意到,如果你输入了funny
,当内部数组被声明为 3 时, strlen(str)
将是 5。所以这暗示着可能存在错误......
接下来,您想要进行以 10 为底的分解,因此我希望您的代码中永远不会出现除数和余数 10。
请记住:您最好的朋友将是一张纸和一支铅笔。 用伪代码或简单地用数学运算符手工写下你想做的事情。 我预计:
一旦这部分完成(并且只有那时),是时候将 go 回到您的键盘了......
首先是这个scanf
调用的参数类型
scanf("%s", &str);
是不正确的。 你至少需要写
scanf("%s", str);
这个数组声明和对应的for循环
int i;
int ascii[50];
for(i = 0; i < strlen(str); i++){
ascii[i] = str[i]; //converting string to ascii
}
是多余的。 您可以直接使用数组
int asc[50][3];
而且这个说法
ascii[i] = str[i];
应该像这样重写
ascii[i] = ( unsigned char )str[i];
这些嵌套的 for 循环
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("-");
}
}
无效,因为数组asc
的第二个索引必须在[0, 3 )
范围内。
该程序可以如下所示
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { N = 50 };
char s[N];
s[0] = '\0';
printf( "Input: " );
scanf( "%49s", s );
size_t n = strlen( s );
unsigned char a[n][3];
for ( size_t i = 0; i < n; i++ )
{
const unsigned char Base = 10;
memset( a[i], 0, sizeof( a[i] ) );
unsigned char c = s[i];
for ( size_t j = sizeof( a[i] ); c; c /= Base )
{
a[i][--j] = c % Base + '0';
}
}
for ( size_t i = 0; i < n; i++ )
{
if ( i != 0 )
{
putchar( '-' );
putchar( '-' );
}
for ( size_t j = 0; j < sizeof( a[i] ); j++ )
{
if ( j != 0 ) putchar( '-' );
putchar( a[i][j] );
}
}
putchar( '\n' );
}
程序 output 可能是
Input: fun
1-0-2--1-1-7--1-1-0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.