gpt4 book ai didi

c - 如何读取两个空格之间的数字?

转载 作者:行者123 更新时间:2023-11-30 15:36:14 26 4
gpt4 key购买 nike

我有一个问题。假设我有

char a[50]={" 99 98 100 97 101 "};

我想要另一个字符串数组,它给我这样的值:

char b[50]={"bcdae"};

那该怎么办?

(a 的 ASCII 值 = 97 等)

最佳答案

#include <stdio.h>

int main() {
char a[50]={" 99 98 100 97 101 "};
char b[50]={0};
char *p = b; // p now points to b
int i, num, pos = 0;

// %d returns each number
// %n returns the number of bytes consumed up to that point

while(sscanf(a + pos, "%d%n", &num, &i)==1)
{
*p++ = (char)num; // b[0]..b[n] are set to num which is cast into a char
pos += i; // increment the position by bytes read
}
printf("%s\n", b);//cbdae
return 0;
}

关于c - 如何读取两个空格之间的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22615392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com