简体   繁体   中英

Sequential letters in C#

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.

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

char c = 'a';

while(c <= 'z')
{
     // Do something with c
     c++;
}

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.

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 az 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?

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'.

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

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;

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 az, you change it to an a and then add an a to the stack.

I won't give you the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM