-2

If the input is 6789 the output should be 6,7,8,9,67,78,89,678,789,6789

The logic should work for large numbers Like 67890,12345678 etc

    static Int64 substrings(string n)
    {
        string p = string.Empty, q = string.Empty, r = string.Empty;
        char[] c = n.ToCharArray();
        string s = string.Empty;
        Int64 res = 0;

        for (int x = 0; x < n.Length; x++)
        {
            res += Convert.ToInt64(n[x].ToString());
        }

        for (int x = 0; x < n.Length; x++)
        {
            if (x > 0)
            {


                p = n[x - 1].ToString();
                q = n[x].ToString();
                r = p + "" + q;

                p = q;
                q = r[0].ToString();

                res += Convert.ToInt64(r);
            }
        }

        for(int x = 0; x < n.Length; x++)
        {
            if (n.Length > 2)
            {
                s += n[x];
            }
        }
        if (n.Length > 2)
        {
            return res + Convert.ToInt64(s);
        }
        else
        {
            return res;
        }
    }

This is the code I have written for generating substrings. This logic doesn't work large numbers

venson
  • 19
  • 5
  • Possible duplicate of: https://stackoverflow.com/questions/756055/listing-all-permutations-of-a-string-integer – Rahul Sharma Mar 24 '19 at 06:37
  • My logic works only for 3 digit number. Ex 123 otuput is 1,2,3,12,23,123. I have used the logic which is used for fibbonoci series – venson Mar 24 '19 at 06:38
  • 1
    What is the code you are having trouble with? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Please, provide a [mcve]. – Jörg W Mittag Mar 24 '19 at 06:41
  • 1
    Your text implies the output should be an array, but your code implies the output should be an `Int64` - so which one is it, then? – Zohar Peled Mar 24 '19 at 06:58
  • I have written code to find sum of all substrings. I have used Int64 so that it will not cause any trouble in case of large numbers. The logic didn't work for number 6789. – venson Mar 24 '19 at 07:02

2 Answers2

1

you could create extension and use yield like this:

static class StringExtensions
{
    public static IEnumerable<String> SplitInMultipleParts(this String s)
    {
        if (s == null)
            throw new ArgumentNullException("s");

        for (var p = 0; p < s.Length; p++)
        {
            for (var i = 0; i < s.Length - p; i++)
                yield return s.Substring(i, p + 1);
        }
    }
}

you call it:

        string num = "123456789";
        var result = num.SplitInMultipleParts().Select(x => Convert.ToInt64(x));

your result is an enumeration of int64, if you want list, just add extension .ToList()

if you dont like Extension, i think its not a problem for you to convert to simple method...

public static IEnumerable<String> SplitInMultipleParts(String s)

ans call it : SplitInMultipleParts(num)
Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • While the answer is correct, why do you see the need to make it an extension method!? The case is pretty specific if you ask me, and im realtively sure its a homeworkquestion. – CSharpie Mar 24 '19 at 07:32
  • @CSharpie, extension method or not is not the question, the main thing is the analysis, i think its not complex to convert the extension to a simple loop or method – Frenchy Mar 24 '19 at 07:38
0

Try this:

IEnumerable<string> Subsequences(string input)
{
    return
        Enumerable
            .Range(0, input.Length)
            .SelectMany(x =>
                Enumerable
                    .Range(1, input.Length - x),
                (x, y) => input.Substring(x, y));
}

With Subsequences("6789") that gives:

6 
67 
678 
6789 
7 
78 
789 
8 
89 
9 
Enigmativity
  • 113,464
  • 11
  • 89
  • 172