1

I'm brand new to C# and I've been absolutely stumped by a problem.

I'm trying to add each digit of a user input in a console app that are the same length (ex: Number 1 = 123 & Number 2 = 456). So I want to to check and see if 1+4=2+5=3+6 and then say if it's true or false.

My problem is, I don't know how to single out each individual digit that has been input. I feel like I'm absolutely overthinking this, but any help would be appreciated.

solatium
  • 37
  • 4
  • 2
    Do you have any code? You do not wan't use to give you the whole thing but seems interested by a part of it, so show us what you have already done! [mcve], [ask]. – Drag and Drop Oct 11 '17 at 07:26
  • User input is always a `string` and you can access individual characters of a string pretty easily – UnholySheep Oct 11 '17 at 07:28
  • 1
    Let's take `456`; `456 % 10 == 6`; `456 / 10 % 10 == 5`, `456 / 10 / 10 % 10 = 4`... Can you see the pattern? Another possibility is `456.ToString()[i] - '0'` where `i = 0, 1, 2` – Dmitry Bychenko Oct 11 '17 at 07:28
  • Check this: https://stackoverflow.com/questions/38631909/int-to-char-array. It will answer your question. – Marco Oct 11 '17 at 07:31
  • https://stackoverflow.com/questions/829174/is-there-an-easy-way-to-turn-an-int-into-an-array-of-ints-of-each-digit – Nino Oct 11 '17 at 07:35

3 Answers3

4

Let's solve the problem in general case and in the most accurate way. First we should answer two questions:

  1. What shall we do with negative numbers? Just ignore the minus - or return false?
  2. What if numbers have different lengths, e.g. 52 and 3? Shall we pad 3 to 03 (and thus return true) or return false?

Implementation

  public static bool Solution(int left, right) {
    // Comment this out (drop) if you accept negative numbers
    if (left < 0 || right < 0)
      return false;

    // Trim('-') - trimming '-' if we have negative numbers
    string A = left.ToString(CultureInfo.InvariantCulture).Trim('-');
    string B = right.ToString(CultureInfo.InvariantCulture).Trim('-');

    // Comment this out (drop) if you want to pad numbers with different lengths
    if (A.Length != B.Length)
      return false;

    // Padding with zeroes if required
    if (A.Length > B.Length)
      B = B.PadLeft(A.Length, '0');
    else if (B.Length > A.Length)
      A = A.PadLeft(B.Length, '0');

    // Finally, we can check
    for (int i = 1; i < A.Length; ++i)
      if (A[i] + B[i] != A[0] + B[0])
        return false; // Do we have a counter example?

    return true;   
  }      
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • `A[n]+B[n] == A[n]B[n]` (concatenated). Need to parse the ints at that point – pinkfloydx33 Oct 11 '17 at 08:32
  • 1
    @pinkfloydx33: no, `A[n]` is of type `char`, that's why `A[n]+B[n]` will be of type `int` (and no concatenation). Technically, it should have been `A[i] - '0' + B[i] - '0' == A[0] - '0' + B[0] - '0'`, but we can simplify and get rid of `'0'` – Dmitry Bychenko Oct 11 '17 at 08:37
3

You can use LINQ:

string number1 = "123";
string number2 = "456";

bool equalSums = number1
    .Zip(number2, (c1, c2) => Convert.ToInt32(c1) + Convert.ToInt32(c2))
    .Distinct()
    .Count() == 1;

Enumerable.Zip links two sequences via index, so char1 of first string is linked with char1 of other string and so on. Convert.ToInt32 converts the character to int and builds the sum of the two digits. Then Distinct removes duplicates and Count checks if there is only one number remaining which means all where equal.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Fails if input have different lengths, e.g. 123 and 10. – Greg Oct 11 '17 at 07:56
  • @Greg: OP has stated that they are always same length, it was one of the requirements: _"....user input in a console app that are the same length"_. But of course you have to precheck, f.e. with `int.TryParse` and `String.Length` – Tim Schmelter Oct 11 '17 at 07:56
  • 1
    You are right, missed that. What is also cool with your solution, you can compare words as well. abc | cba = True – Greg Oct 11 '17 at 08:01
-1

This is a great problem to show the power of regex, assuming your numbers are stored in properties,

string num1 = Console.ReadLine();
string num2 = Console.ReadLine();

 int[] numbers1 = (from Match m in Regex.Matches(num1 , @"\d") select 
 int.Parse(m.Value)).ToArray();

 int[] numbers2 = (from Match m in Regex.Matches(num2 , @"\d") select int.Parse(m.Value)).ToArray();

Then you have two number arrays you can tests out e/g

if (numbers1[1] + numbers2[1] == numbers1[2] + numbers2[2])
{
Console.WriteLine("aye its true");
}
else {
//do something else
}

I hope that answers your question

P.S. I used Regex 101 for testing my regex, its a nice tool

JohnChris
  • 1,360
  • 15
  • 29
  • Using a regex here only complicates the problem. Also instead of `@"\d+"` at least use `@"\d"` (or even `"."`). Give me some time @JohnChris. – heijp06 Oct 11 '17 at 07:51
  • Fair enough. Problem is though, with `"@\d+"` you regex matches the entire string and your array would only contain one member: The original number. – heijp06 Oct 11 '17 at 07:55
  • I didn't downvote but from my reading it's hard to understand your point: your convertion of a int to string, then a select , then a to array.. I'm not sure what happends here. – Drag and Drop Oct 11 '17 at 08:01
  • Then the second part is even more weird. `numbers1[1] + numbers2[1] == numbers1[2] + numbers2[2]` .. is clearly weird. how do you build that? The starting index is 0 not 1 thats disturbing. – Drag and Drop Oct 11 '17 at 08:03
  • I was assuming he kept the values in properties like my answer, said - i removed that fine, I gave an example of how he can test, ignoring the starting array integer, i didnt want to give a full solution, let him think a bit... – JohnChris Oct 11 '17 at 08:05
  • You gave help only on Casting user input into int array. It's a step in the good direction still regex is not what comes first here. I 'm trying to find what would cause a downvote. There is no judging. – Drag and Drop Oct 11 '17 at 08:07
  • Fair enough... and i made a mistake initially with my casting to an array, so all good. – JohnChris Oct 11 '17 at 08:09
  • cleanning up my comment, as they are a bit chatty. – Drag and Drop Oct 11 '17 at 08:47