views:

223

answers:

5

I am having the numbers follows taken as strings

My actual number is 1234567890123456789

from this i have to separate it as s=12 s1=6789 s3=3456789012345

remaining as i said

I would like to add as follows

1+3, 2+4, 6+5, 7+6, 8+7, 9+8 such that the output should be as follows

4613579012345

Any help please

+2  A: 

This sounds an awful lot like a homework problem, so I'm not giving code. Just think about what you need to do. You are saying that you need to take the first character off the front of two strings, parse them to ints, and add them together. Finally, take the result of the addition and append them to the end of a new string. If you write code that follows that path, it should work out fine.

EDIT: As Ralph pointed out, you'll also need to check for overflows. I didn't notice that when I started typing. Although, that shouldn't be too hard, since you're starting with a two one digit numbers. If the number is greater than 9, then you can just subtract 10 to bring it down to the proper one digit number.

fire.eagle
I suggest you put that in a comment, else I'm down voting. This isn't an acceptable answer.
Ruel
And watch out for overflows. It seems from your output that your desired result of 7+6 is 3.
Ralph Rickenbach
I'd vote it up to counter your vote down. It's an answer to a problem. If it's not acceptable, then it is merely one of many answers to problems which aren't accepted, isn't it?
Neil
It's not actually an answer, he just rephrased the question to a recommendation. Long story short, his answer is obviously given.
Ruel
@Ruel - this is an acceptable answer following the rules of answering homework questions. http://meta.stackoverflow.com/questions/10811/how-to-ask-and-answer-homework-questions Actually this question might as well be closed, as there is no visible effort shown to come up with a solution, only PlsGiveMeTheCodez
Ralph Rickenbach
@Ruel - relax dude
Jack Marchetti
@Ruel - The thing is, a lot of the time, this works for people. I help some people who are trying to learn to program at my college, and a lot of the time, they know how to write code; they just have a hard time figuring out how to take the question and turn it into a process that can be implemented. So, I usually try to just work with them about the logic. And yes, usually on the easier problems, it is just basically rephrasing the question. The key is to get them to figure out how to work out the logic on their own. After that's done, the actual coding is simple.
fire.eagle
A: 

In other words, you want to add two numbers treating the lesser number like it had zeroes to its right until it had the same amount of digits as the greater number.

Sounds like the problem at this point is simply a matter of finding out how much you need to multiply the smaller number by in order to reach the number of digits of the larger number.

Neil
except that the numbers are not integer, they are strings of digits, and the addition is mod 10 per digit, not integer addition with carry between digits
Pete Kirkham
+1  A: 

Tried something:

public static string NumAdd(int iOne, int iTwo)
    {
        char[] strOne = iOne.ToString().ToCharArray();
        char[] strTwo = iTwo.ToString().ToCharArray();

        string strReturn = string.Empty;

        for (int i = 0; i < strOne.Length; i++)
        {
            int iFirst = 0;
            if (int.TryParse(strOne[i].ToString(), out iFirst))
            {
                int iSecond = 0;
                if (int.TryParse(strTwo[i].ToString(), out iSecond))
                {
                    strReturn += ((int)(iFirst + iSecond)).ToString();
                }
            }

            // last one, add the remaining string
            if (i + 1 == strOne.Length)
            {
                strReturn += iTwo.ToString().Substring(i+1); 
                break;
            }
        }

        return strReturn;
    }

You should call it like this:

string strBla = NumAdd(12345, 123456789);

This function works only if the first number is smaller than the second one. But this will help you to know how it is about.

cevik
Doesn't take into account two numbers summing to 10 or greater. So 126 and 345 would give you 4611 instead of 461 as desired.
kekekela
Also your text after the code is a little confusing, you say it only works if the first number is greater then the second one, but say "it should be called like..." and give an example with a first number that is smaller than the second one.
kekekela
@kekekela: I already gave an example (look at the second code segment) and this was just a guide to show how you can solve problems like this.
cevik
I am looking at the second code segment, it was what my comment was based on. It calls the function with a first number that is smaller than the second number, even though you turn around and say the first number needs to be greater.
kekekela
you are right! did not see it, thanks :)
cevik
+2  A: 
    public static string CombineNumbers(string number1, string number2)
    {
        int length = number1.Length > number2.Length ? number1.Length : number2.Length;
        string returnValue = string.Empty;

        for (int i = 0; i < length; i++)
        {
            int n1 = i >= number1.Length ? 0 : int.Parse(number1.Substring(i,1));
            int n2 = i >= number2.Length ? 0 : int.Parse(number2.Substring(i,1));
            int sum = n1 + n2;

            returnValue += sum < 10 ? sum : sum - 10;
        }
        return returnValue;
    }
kekekela
+1  A: 

How about this LINQish solution:

private string SumIt(string first, string second)
{
    IEnumerable<char> left = first;
    IEnumerable<char> right = second;

    var sb = new StringBuilder();

    var query = left.Zip(right, (l, r) => new { Left = l, Right = r })
                    .Select(chars => new { Left = int.Parse(chars.Left.ToString()),
                                           Right = int.Parse(chars.Right.ToString()) })
                    .Select(numbers => (numbers.Left + numbers.Right) % 10);

    foreach (var number in query)
    {
        sb.Append(number);
    }
    return sb.ToString();
}
Oliver
Gets the first part correct, but does not include the trailing digits from the longer number as in his example. (ie you get 461357 instead of 4613579012345)
kekekela
@kekekela: Seems that i missed this req while reading the question. Due to the fact, that your answer is already marked as correct i won't make any updates to my code.
Oliver