C# – Conversion of text array to integer array

In the current article I present an interesting problem, concerning conversion of text array to integer array. The casting of values in arrays has always been a problem for people without much experience and with the current text I will try to enlight it through the following example. The problem description is located here.

Pretty much, we should read a long number and compare the sum of its even and its odd digits. Then, if the sum of the odd is higher, we write “left” and the sum. In case of higher sum of the even digits we write “right” and in case of equal sum we write “straight”. Thus, in case “1122” is given, we write “right 4”, due to the fact that 2+2 is higher than 1+1.



So, what is the interesting part of this problem? Actually only one – the conversion of text array to integer array. It is easily done with a for loop and the following code:

So, in the given code, we actually have 3 conversions. First we convert the entered number to long, in order to make it positive if it is negative. Then we convert it to string, because this way it becomes an array. At last we convert the string array to integer array and everyone is happy.

Enjoy the code here:


Tagged with: , , ,