C - Convert String with newlines into Number Array

Thread Starter

corsair

Joined Mar 6, 2010
51
Hi guys,

I'm having trouble figuring out how to convert a string with new lines into an array of ints.

Simply put, it would convert something like this:

Rich (BB code):
input:
char *s = "32\n\1\n\35\6\n2\n";

output
int i[];

i[0] = 32
i[1] = 1
i[2] = 35
i[3] = 6
i[4] = 2
this is a pseudocode of what id like:

Rich (BB code):
Parse(char input[100]) {
char *c = input;
int i = 0;
char output[100]

while (c != '\0') { //does this count as one or two chars?

if (c != '\n') {
//put number into output array indexed at i, my problem here is that numbers can have 1-4 digits
i++;
}
c++
}

}
 

Papabravo

Joined Feb 24, 2006
21,228
Converting characters in a string to a number is easy

  1. Set an integer "number" to 0
  2. For each character that is not a newline, multiply "number" by 10 and add the quantity "character" minus the value of the character constant '0'
  3. When you get to the newline store "number" in the array
'\0' is a so called escape sequence and it evaluates to a single character with the value zero.
 

AsmCoder8088

Joined Apr 17, 2010
15
Try this:

Rich (BB code):
#include <stdio.h>
#include <stdlib.h>

struct NUMBER
{
    int nNumber;
    struct NUMBER *pPrev;
    struct NUMBER *pNext;
};

struct LISTNUM
{
    struct NUMBER *pHead;
    struct NUMBER *pTail;
    struct NUMBER *pCurrent;
};

int main(int argc, char *szArgv[])
{
    FILE *pFile;
    struct LISTNUM listNum;
    char ch;
    char isNeg;
    int nNumber;

    if (argc != 2)
    {
        printf("Usage: %s <input file>\n", szArgv[0]);
        return 0;
    }

    pFile = fopen(szArgv[1], "rb");

    if (!pFile)
    {
        printf("Unable to open input file\n");
        return 0;
    }

    // Read in the list of numbers
    listNum.pHead = (struct NUMBER *)malloc(sizeof(struct NUMBER));
    listNum.pTail = (struct NUMBER *)malloc(sizeof(struct NUMBER));
    listNum.pHead->pPrev = listNum.pHead;
    listNum.pHead->pNext = listNum.pTail;
    listNum.pTail->pPrev = listNum.pHead;
    listNum.pTail->pNext = listNum.pTail;
    listNum.pCurrent = listNum.pHead;

    nNumber = 0;
    isNeg = 0;

    while (1)
    {
        ch = fgetc(pFile);

        if (ch == '-')
        {
            isNeg = 1;
            continue;
        }
        else if (ch == '\r')
        {
            continue;
        }
        else if (ch == '\n' || feof(pFile))
        {
            if (isNeg)
            {
                isNeg = 0;
                nNumber = -nNumber;
            }

            if (listNum.pCurrent->pNext != listNum.pTail)
            {
                listNum.pCurrent->pNext->nNumber = nNumber;
                listNum.pCurrent->pNext->pPrev = listNum.pCurrent;
                listNum.pCurrent->pNext->pNext = listNum.pTail;
                listNum.pTail->pPrev = listNum.pCurrent->pNext;
                listNum.pCurrent = listNum.pCurrent->pNext;
            }
            else
            {
                break;
            }

            nNumber = 0;
        }
        else
        {
            nNumber *= 10;
            nNumber += ch - '0';

            listNum.pCurrent->pNext = (struct NUMBER *)malloc(sizeof(struct NUMBER));
        }
    }

    fclose(pFile);

    listNum.pCurrent = listNum.pHead->pNext;
    while (listNum.pCurrent != listNum.pTail)
    {
        printf("Number: %i\n", listNum.pCurrent->nNumber);

        listNum.pCurrent = listNum.pCurrent->pNext;
    }

    listNum.pCurrent = listNum.pTail->pPrev;

    return 0;
}
The above will read in an arbitrary number of integers. Works with negative numbers as well.

Hope this helps,

Daniel
 
Top