2

I want to generate sequential letters in C#.

For example the first value of a variable would be "a", followed by "b", "c", ... After "z", the range would continue with "aa", "bb", ...

Something like

i=0;
while(i<40)
{
   Console.WriteLine(i);
   i++;
}

but using letters.

Kevin Montrose
  • 22,191
  • 9
  • 88
  • 137
Eduarth
  • 31
  • 1
  • 2
  • Is this a homework assignment? Or perhaps an interview question? – cost May 09 '11 at 18:31
  • 1
    Please post what you have tried so far so we can help you improve existing code - you won't learn unless you try it for yourself. – JasCav May 09 '11 at 18:31
  • Eduarth, if any of the posted answers solved your problem you should accept it (click on the check mark left of the answer). – Florian Greinacher May 10 '11 at 09:17
  • The accepted answer to this question is by far the most elegant I have seen: [in c#, how can i build up array from A to ZZ that is similar to the way that excel orders columns](https://stackoverflow.com/questions/5384554/in-c-how-can-i-build-up-array-from-a-to-zz-that-is-similar-to-the-way-that-exc) – SpruceMoose Jan 19 '18 at 14:38

8 Answers8

6

You can increment a character as you can increment an integer.

char c = 'a';

while(c <= 'z')
{
     // Do something with c
     c++;
}
Florian Greinacher
  • 14,478
  • 1
  • 35
  • 53
3

Note, i just whipped this up. I haven't tried it.

for (int i=0; i<40; ++i)
{
    char digit = (char) (97 + i%26);  // utf/ascii code 97 == 'a'
    Console.WriteLine(new String(digit, i/26 + 1));
}

The String(char c, int n) constructor gives you back a string with the char c repeated n times. From there, all you need is what to repeat ('a' for 0, 26, 52, etc), and how many times to repeat it (1 for 0, 2 for 26, 3 for 52, etc).

Also note, i can be anything (well, any positive number). I just looped from 0 to 40 as you were doing. You don't have to work up to it or store intermediate results.

cHao
  • 84,970
  • 20
  • 145
  • 172
1

Since you've added a new homework tag, I'm not going to give you the outright answer, but I'll give you a point in the right direction. Like Florian Greinacher said, you can 'count' though ascii characters just like you would numbers (actually, I think you can, I've never done this with C#. Does the strong typing allow it?).

Use Florian's advice, but keep track of how many times you have completed the a-z loop, and print out more copies of what you need through more iterations.

Do you need to print out things like "ab" and "ba," or just "aa" "aaa" etc?

cost
  • 4,420
  • 8
  • 48
  • 80
0

Psuedo code:

Declare a base string
Inialize base string to ""
for each letter in alphabet
    print base_string + letter
    if(letter == last letter)
       base_string = base_string + letter
       letter = first letter

next letter
Pepe
  • 6,360
  • 5
  • 27
  • 29
0

I would declare an array of 26 characters, where [0] = a, [1] = b, etc. Then, for a given integer, divide by 26, and store the quotient and mod values as variables. The answer will be the element at position (mod-1), printed out (quotient + 1) times. This code below isn't perfect; you'll probably have to use a cast to make the ints play nicely. Also, I'm using the number 92 for the purpose of this example, but you could use any (number)...

string[] letters = {'a', 'b', ... };
int number = 92;
int quotient = number / 26; // answer is 3
int mod = number % 26; // answer is 14
string answer = "";

for (int i = 0; i <= quotient; i ++) {
    answer += letters[mod - 1];
}

return answer;
WEFX
  • 8,298
  • 8
  • 66
  • 102
  • A bit messier than it has to be, but not bad. :) If you're using 92 as the example, though, i'd suggest using the variable (`number`) rather than the literal. Makes the steps more obvious. – cHao May 09 '11 at 18:59
  • You're right @cHao, I declared that (number), but then forgot to reference it. I've edited it now. Thanks – WEFX May 09 '11 at 19:03
0

It is a pretty easy job. As far as I can see all other answers are over-complicated.

using Xunit;

[Fact]
public void CharSequenceTest()
{
    var seq = Enumerable.Range('a', 5).Select(x => (char)x);

    Assert.Equal("abcde", string.Concat(seq));
}
ADM-IT
  • 3,719
  • 1
  • 25
  • 26
-1

You can convert an int to a char (like so).
This way you can use a for loop on an int.
You can use an if statement to check if your int exceeded z's value, and if so subtract z's value from your int, to find out what's the difference, which you can use to write the double chars.
I would advice you against looping on a char, since it might get messy once you passed 'z'.

Community
  • 1
  • 1
Oren A
  • 5,870
  • 6
  • 43
  • 64
-1

This problem works great with a stack data structure. Every time you increment a letter, you either increment the item on top or change it to an 'a' and then change the next item. If you get to the base letter and it is a z, you change it to an a and then add an a to the stack.

I won't give you the code.

Troy S.
  • 9
  • 1
  • 6