c - How to read number between two spaces? -
i have problem. suppose have
char a[50]={" 99 98 100 97 101 "};
and want array of string gives me values this:
char b[50]={"bcdae"};
then do?
(ascii value of = 97 , on)
#include <stdio.h> int main() { char a[50]={" 99 98 100 97 101 "}; char b[50]={0}; char *p = b; // p points b int i, num, pos = 0; // %d returns each number // %n returns number of bytes consumed point while(sscanf(a + pos, "%d%n", &num, &i)==1) { *p++ = (char)num; // b[0]..b[n] set num cast char pos += i; // increment position bytes read } printf("%s\n", b);//cbdae return 0; }
Comments
Post a Comment