C - Convert String with newlines into Number Array

Discussion in 'Programmer's Corner' started by corsair, Nov 28, 2010.

  1. corsair

    Thread Starter Member

    Mar 6, 2010
    51
    1
    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:

    Code ( (Unknown Language)):
    1. input:
    2. char *s = "32\n\1\n\35\6\n2\n";
    3.  
    4. output
    5. int i[];
    6.  
    7. i[0] = 32
    8. i[1] = 1
    9. i[2] = 35
    10. i[3] = 6
    11. i[4] = 2
    this is a pseudocode of what id like:

    Code ( (Unknown Language)):
    1.  
    2. Parse(char input[100]) {
    3. char *c = input;
    4. int i = 0;
    5. char output[100]
    6.  
    7. while (c != '\0') { //does this count as one or two chars?
    8.  
    9. if (c != '\n') {
    10. //put number into output array indexed at i, my problem here is that numbers can have 1-4 digits
    11. i++;
    12. }
    13. c++
    14. }
    15.  
    16. }
    17.  
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    This sounds suspiciously like a homework assignment to me. :)


    For starters take a look at the strtok function.
     
  3. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    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.
     
  4. AsmCoder8088

    New Member

    Apr 17, 2010
    15
    1
    Try this:

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4.  
    5. struct NUMBER
    6. {
    7.     int nNumber;
    8.     struct NUMBER *pPrev;
    9.     struct NUMBER *pNext;
    10. };
    11.  
    12. struct LISTNUM
    13. {
    14.     struct NUMBER *pHead;
    15.     struct NUMBER *pTail;
    16.     struct NUMBER *pCurrent;
    17. };
    18.  
    19. int main(int argc, char *szArgv[])
    20. {
    21.     FILE *pFile;
    22.     struct LISTNUM listNum;
    23.     char ch;
    24.     char isNeg;
    25.     int nNumber;
    26.  
    27.     if (argc != 2)
    28.     {
    29.         printf("Usage: %s <input file>\n", szArgv[0]);
    30.         return 0;
    31.     }
    32.  
    33.     pFile = fopen(szArgv[1], "rb");
    34.  
    35.     if (!pFile)
    36.     {
    37.         printf("Unable to open input file\n");
    38.         return 0;
    39.     }
    40.  
    41.     // Read in the list of numbers
    42.     listNum.pHead = (struct NUMBER *)malloc(sizeof(struct NUMBER));
    43.     listNum.pTail = (struct NUMBER *)malloc(sizeof(struct NUMBER));
    44.     listNum.pHead->pPrev = listNum.pHead;
    45.     listNum.pHead->pNext = listNum.pTail;
    46.     listNum.pTail->pPrev = listNum.pHead;
    47.     listNum.pTail->pNext = listNum.pTail;
    48.     listNum.pCurrent = listNum.pHead;
    49.  
    50.     nNumber = 0;
    51.     isNeg = 0;
    52.  
    53.     while (1)
    54.     {
    55.         ch = fgetc(pFile);
    56.  
    57.         if (ch == '-')
    58.         {
    59.             isNeg = 1;
    60.             continue;
    61.         }
    62.         else if (ch == '\r')
    63.         {
    64.             continue;
    65.         }
    66.         else if (ch == '\n' || feof(pFile))
    67.         {
    68.             if (isNeg)
    69.             {
    70.                 isNeg = 0;
    71.                 nNumber = -nNumber;
    72.             }
    73.  
    74.             if (listNum.pCurrent->pNext != listNum.pTail)
    75.             {
    76.                 listNum.pCurrent->pNext->nNumber = nNumber;
    77.                 listNum.pCurrent->pNext->pPrev = listNum.pCurrent;
    78.                 listNum.pCurrent->pNext->pNext = listNum.pTail;
    79.                 listNum.pTail->pPrev = listNum.pCurrent->pNext;
    80.                 listNum.pCurrent = listNum.pCurrent->pNext;
    81.             }
    82.             else
    83.             {
    84.                 break;
    85.             }
    86.  
    87.             nNumber = 0;
    88.         }
    89.         else
    90.         {
    91.             nNumber *= 10;
    92.             nNumber += ch - '0';
    93.  
    94.             listNum.pCurrent->pNext = (struct NUMBER *)malloc(sizeof(struct NUMBER));
    95.         }
    96.     }
    97.  
    98.     fclose(pFile);
    99.  
    100.     listNum.pCurrent = listNum.pHead->pNext;
    101.     while (listNum.pCurrent != listNum.pTail)
    102.     {
    103.         printf("Number: %i\n", listNum.pCurrent->nNumber);
    104.  
    105.         listNum.pCurrent = listNum.pCurrent->pNext;
    106.     }
    107.  
    108.     listNum.pCurrent = listNum.pTail->pPrev;
    109.  
    110.     return 0;
    111. }
    112.  
    The above will read in an arbitrary number of integers. Works with negative numbers as well.

    Hope this helps,

    Daniel
     
Loading...