0

I have a problem. Suppose I have

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

and I want an another array of string that gives me values like this:

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

then what to do?

(ASCII value of a = 97 and so on)

Maroun
  • 94,125
  • 30
  • 188
  • 241

3 Answers3

1
#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;
}
Engineer2021
  • 3,288
  • 6
  • 29
  • 51
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
1

The following initializations don't seem like what you meant (although they compile):

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

If you meant:

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

Then the contents of the two arrays are identical.


If you meant:

char a[50] = {99 , 98 , 100,  97, 101};
char b[50] = {'b', 'c', 'd', 'a', 'e'};

Then the contents of the two arrays are identical.


If you meant:

char a[50] = " 99 98 100 97 101 ";
char b[50] = "bcdae";

Which is equivalent to what you posted, then you can use this:

#include "string.h"

void a2b(char a[],char b[])
{
    int i = 0, j;
    char* pch = strtok(a," ");
    while (pch != NULL)
    {
        b[i] = 0;
        for (j=0; pch[j]!=0; j++)
        {
            b[i] *= 10;
            b[i] += pch[j]-'0';
        }
        i++;
        pch = strtok(NULL," ");
    }
    b[i] = 0;
}

Please note, however, that the above code changes the contents of the first array.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • `char a[50]={" 99 98 100 97 101 "};` is valid. _An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array._ 6.7.8 Initializaiton 14 – BLUEPIXY Mar 24 '14 at 17:54
  • @BLUEPIXY: Thanks. 1. It still "smells" like OP meant one of the first two options mentioned above. 2. The third option mentioned above gives a solution in case this is indeed what OP meant. – barak manos Mar 24 '14 at 17:56
  • _The following initializations don't seem right_ Please delete or modify. In addition, the meaning is clear from the fact that my answer has been accepted already. – BLUEPIXY Mar 24 '14 at 17:58
  • @BLUEPIXY: Is this better? – barak manos Mar 24 '14 at 18:00
-1

"bcdae" is nothing else than an array of numbers: [99, 98, 100, 97, 101, 0]. After all, there are no "letters" in the computer memory, just numbers. The last number 0 denotes the end of the string.

So, your task, seems to be a matter of repeatedly reading a number from a string (check scanf function), and putting the read value into an array of numbers.


Hint: Learning the amount of characters you read when parsing a number may be helpful - check Get number of characters read by sscanf?

Community
  • 1
  • 1
CygnusX1
  • 20,968
  • 5
  • 65
  • 109
  • Psh, there are no number in computer memory, just electronic charges and the lack thereof. – SGM1 Mar 24 '14 at 17:00