converting string to int back to string

This is a discussion on converting string to int back to string within the C Programming forums, part of the General Programming Boards category; User will enter 2 strings from console ie 2342223423 and 23422 and I have to add them up as if ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    converting string to int back to string

    User will enter 2 strings from console ie 2342223423 and 23422

    and I have to add them up as if they were integers.

    I already have the code that will fill in the zeros like 2342223423 and 0000023422 so the addition can be done easily.

    what I have been doing is I created a temporary int array that stores the values of the two strings being added by using the atoi method and seem to be having issues with it.

    Also trying to use the itoa as well.

    my method head is large_sum( char * num1, char *num2, char *answer)

    where i have to return answer back to the main method to print it.

    I have done this in java but used linked lists but I am forced to do C and use arrays.

    any suggestions as to why I am having issues with the atoi then itoa ?

    I just want to basically parse the string from the end to front, into ints, add them.. take that value and convert back into a char and put it into the answer array which is a string...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    If I remember correctly, itoa() is non-standard.

    You could just use atoi() to convert the two strings to int, add them, and then use sprintf() to convert the result to a string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have any concern as to error handling of the input, I would recommend the more tolerant strtol() function for "string to integer".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    Quote Originally Posted by laserlight View Post
    If I remember correctly, itoa() is non-standard.

    You could just use atoi() to convert the two strings to int, add them, and then use sprintf() to convert the result to a string.
    Yeah but the reason for this method is to be able to take an infinite sized number and be able to add them.

    so I you can add 23429834290189012387340129342134129083412342134012 98471239047123421304892734 + 2340298347129084719028421390412341232341123

    and not worry about any over flows...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Oh, so you are writing a simple bignum calculator.

    In this case atoi() and strtol() are unnecessary. Just note that given a char c that contains a digit, c - '0' returns the actual value of that digit. For example, if c = '5' then c - '0' = 5.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21