Separate the Numbers

  • + 18 comments

    Case 1: s=1234

    In the first iteration of the for loop, we consider the first digit in the number.So we take 1. Now concat the number to string Test.

    • Test=1.Then added 1 to it=>2. Then concatinate to Test.
    • Test=12. Then added 1 again. Concatinate to Test
    • Test=123. Then added 1 again. Concatinate to Test
    • Test=1234. Now the length of test and s are equal. So we compare the two strings. If they are equal, we have the soluton.

    Case 2: s=121314

    We do the steps as above. But after the first iteration in the for loop Test=123456. When we compare, the strings are not equal. So continuing to second iteration of for loop... Now we take the first TWO digits,ie,12. Then, as stated above, we add 1 and concatinate until length of s and Test are the same. So,

    • Test=12
    • Test=1213
    • Test=121314. Now we compare the lengths. They are the same. So we compare the strings. They are equal. Hence the solution.