1

So, what I'm trying to do this something like this: (example)

a,b,c,d.. etc. aa,ab,ac.. etc. ba,bb,bc, etc.

So, this can essentially be explained as generally increasing and just printing all possible variations, starting at a. So far, I've been able to do it with one letter, starting out like this:

for (int i = 97; i <= 122; i++)
{
    item = (char)i
}

But, I'm unable to eventually add the second letter, third letter, and so forth. Is anyone able to provide input? Thanks.

Jakexx360
  • 25
  • 3
  • 5
    Hint: Use... more... loops... – Oded Jul 09 '12 at 20:38
  • 7
    Is this homework? – Miserable Variable Jul 09 '12 at 20:38
  • 3
    Put your code inside a function. Call this function from itself. It is called "recursion". Happy hacking! – LosManos Jul 09 '12 at 20:39
  • Nope, not homework, and yes, I've gone into over 6 loops, got too complicated to work – Jakexx360 Jul 09 '12 at 20:40
  • 1) Please be sure to mark this as "Homework" if this is a homework assignment. 2) I'm not sure if this is relevant for your class ... but in the "real world", you should *ALWAYS* use "Text.StringBuilder" when you're doing a lot of character-by-character text manipulation. Dealing with "String" itself is just too inefficient: http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx. PS: Here's a good article on Permutations and combinations in C#: http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G – paulsm4 Jul 09 '12 at 20:42
  • Why is this widely expected to be a homework assignment? Seeing as I only picked up c# about 2 hours ago, I'm using this as practice. Nothing major – Jakexx360 Jul 09 '12 at 20:43
  • 3
    With _ALWAYS_ paulsm4 of course means _sometimes_ or _when needed_ http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html – LosManos Jul 09 '12 at 20:44
  • @Jakexx360 To be honest, this program won't be quite as trivial as you might have thought. You might want to pick something a bit simpler for just starting out. Maybe table this one for a few weeks. – Servy Jul 09 '12 at 20:54

4 Answers4

2

Since there hasn't been a solution so far that would literally "increment a string", here is one that does:

static string Increment(string s) {
    if (s.All(c => c == 'z')) {
        return new string('a', s.Length + 1);
    }
    var res = s.ToCharArray();
    var pos = res.Length - 1;
    do {
        if (res[pos] != 'z') {
            res[pos]++;
            break;
        }
        res[pos--] = 'a';
    } while (true);
    return new string(res);
}

The idea is simple: pretend that letters are your digits, and do an increment the way they teach in an elementary school. Start from the rightmost "digit", and increment it. If you hit a nine (which is 'z' in our system), move on to the prior digit; otherwise, you are done incrementing.

The obvious special case is when the "number" is composed entirely of nines. This is when your "counter" needs to roll to the next size up, and add a "digit". This special condition is checked at the beginning of the method: if the string is composed of N letters 'z', a string of N+1 letter 'a's is returned.

Here is a link to a quick demonstration of this code on ideone.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Each iteration of Your for loop is completely overwriting what is in "item" - the for loop is just assigning one character "i" at a time

If item is a String, Use something like this:

item = "";
for (int i = 97; i <= 122; i++)
{
  item += (char)i;
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
A B
  • 4,068
  • 1
  • 20
  • 23
0

something to the affect of

public string IncrementString(string value)
{
    if (string.IsNullOrEmpty(value)) return "a";

    var chars = value.ToArray();
    var last = chars.Last();

    if(char.ToByte() == 122)
    return value + "a";

    return value.SubString(0, value.Length) + (char)(char.ToByte()+1);
}

you'll probably need to convert the char to a byte. That can be encapsulated in an extension method like static int ToByte(this char);

StringBuilder is a better choice when building large amounts of strings. so you may want to consider using that instead of string concatenation.

Jason Meckley
  • 7,589
  • 1
  • 24
  • 45
0

Another way to look at this is that you want to count in base 26. The computer is very good at counting and since it always has to convert from base 2 (binary), which is the way it stores values, to base 10 (decimal--the number system you and I generally think in), converting to different number bases is also very easy.

There's a general base converter here https://stackoverflow.com/a/3265796/351385 which converts an array of bytes to an arbitrary base. Once you have a good understanding of number bases and can understand that code, it's a simple matter to create a base 26 counter that counts in binary, but converts to base 26 for display.

Community
  • 1
  • 1
Tergiver
  • 14,171
  • 3
  • 41
  • 68