# String generator

This topic is 3382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! I need to make every number a string. They go: 1 = "A" 26 = "Z" 27 = "AA" 28 = "AB" and so on. Any idea? I'm stuck. All I've gotten untill now has been un-useable! I apprechiate any help! Regards!

##### Share on other sites
What have you done so far?

##### Share on other sites
Well, here's what I'm at now.
I've started over and over on this one.

(Sorry, I don't know how yo put code in one of those boxes.)

public static string GetUniqueCode(int number)
{
int iNumber = number;

if (iNumber <= 0)
throw new ArgumentOutOfRangeException();

int counter = 1;
string returnString = "";
const int UPPERCHARPOSITION = 65;
while (true)
{
if (Math.Pow(26, counter) > iNumber)
{
for(int i = 0;i < counter; i++)
{
returnString = Convert.ToChar((int)(iNumber % Math.Pow(26, i)) + UPPERCHARPOSITION).ToString() + returnString;
}
break;
}
counter++;
}
return returnString;
}

private void button1_Click(object sender, EventArgs e)
{
label1.Text = GetUniqueCode(Convert.ToInt32(textBox1.Text));
}

##### Share on other sites

2. Use recursion:
f(x) = string consisting of the x-th character, if x < 26
f(x) = f(x / 26 - 1) + f(x % 26), else

Or even better recursion (I like trivial base cases):
f(x) = empty string, if x < 0
f(x) = f(x / 26 - 1) + (x % 26)-th character, else

non-optimized C++ code:
#include <iostream>#include <string>std::string unique_code(int x){    return x < 0 ? "" : unique_code(x / 26 - 1) + char('A' + (x % 26));}int main(){    std::cout << unique_code(0)     << std::endl;    std::cout << unique_code(25)    << std::endl;    std::cout << unique_code(26)    << std::endl;    std::cout << unique_code(27)    << std::endl;    std::cout << unique_code(26*26) << std::endl;    std::cout << unique_code(27*26) << std::endl;}

##### Share on other sites
I can see it working with your c++ code, but when I try to
transer it to c#-code it's getting messed up.
I can't make a call for a function in that same function.
I get a "StackOverFlow".

Any ideas?:)
Btw - thanks for all your help so far!:)

##### Share on other sites
Quote:
 Original post by C3rialwhen I try to transer it to c#-code it's getting messed up.

Well, show us the code then.

Quote:
 Original post by C3rialI get a "StackOverFlow".

A StackOverflow is usually an indication that you forgot your base case and thus never stop the recursion. Again, show us the code.

##### Share on other sites
Hi again!
I got it working eventually:D
Sorry about forgetting the code and all.

Thanks alot for all your help! Recursion is great:D

Regards!