This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Add two strings

Hallo,
Is it possible, that I can add the strings '1' '.' and '2'.
I want to add this 3 strings: 1.2 and convert it into a float value and than I want add a float value.

Parents
  • Sounds more like a class assignment. Given two numbers, which happen to be represented as strings, add them together.

    It is possible to do this without converting the strings to integers and back. (Though, using existing libraries likely would be the easiest and fastest way!). You could write code to loop over the strings, from the end to the beginning (least significant characters first), adding each individual pair of characters, and keeping the result as a character. That is, the same algorithm you were probably taught to use when doing the arithmetic by hand.

    Skipping all sorts of error and bounds checking, this should do:
    sprintf (outStr, "%d", atoi(inStr1) + atoi(inStr2));

Reply
  • Sounds more like a class assignment. Given two numbers, which happen to be represented as strings, add them together.

    It is possible to do this without converting the strings to integers and back. (Though, using existing libraries likely would be the easiest and fastest way!). You could write code to loop over the strings, from the end to the beginning (least significant characters first), adding each individual pair of characters, and keeping the result as a character. That is, the same algorithm you were probably taught to use when doing the arithmetic by hand.

    Skipping all sorts of error and bounds checking, this should do:
    sprintf (outStr, "%d", atoi(inStr1) + atoi(inStr2));

Children
No data