locked
C# increment letter!?! RRS feed

  • Question

  •  

    Hello,

    I am new to C# but i do know other programming languages such as PHP PERL ASP JAVA etc. so i get the basics of C#. My problem is that I have been trying to make a letter increment as such:

    a-b-c-d etc then when it gets to z go: aa-ab-ac-ad... ba-bb-bc-bd and so on.

     

    If anyone knows a simple solution to this please respond asap. I will keep trying but I've gotten nowhere in the past week.

     

    Thank You,

    Osiris

    Thursday, October 18, 2007 12:34 AM

Answers

  • Or if you want something to really 'increment' letters rather than having to convert back and forth to numbers, here is a simple recursive app:

    Code Block

    class Class1

    {

    [STAThread]

    static void Main(string[] args)

    {

    string s = "a";

    while (s.Length < 5) // for this demonstration we'll just go up to 'zzzz'

    {

    Console.WriteLine(s);

    s = Increment(s);

    }

    }

     

    static string Increment(string s)

    {

    // first case - string is empty: return "a"

    if ((s == null) || (s.Length == 0))

    return "a";

    // next case - last char is less than 'z': simply increment last char

    char lastChar = s[s.Length - 1];

    string fragment = s.Substring(0, s.Length - 1);

    if (lastChar < 'z')

    {

    ++lastChar;

    return fragment + lastChar;

    }

    // next case - last char is 'z': roll over and increment preceding string

    return Increment(fragment) + 'a';

    }

    }

     

    Thursday, October 18, 2007 2:36 PM
  • Perhaps this will help?

     

    Code Block

    public string ConvertDecString(int value)

    {

    string CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    string retVal = string.Empty;

    double value = Convert.ToDouble(dvalue);

     

    do

    {

    double remainder = value - (26 * Math.Truncate(value / 26));

    retVal = retVal + CHARS.Substring((int)remainder, 1);

    value = Math.Truncate(value / 26);

    } while (value > 0);

     

    return retVal;

    }

     

     

     

    To use it, just pass it a value and you will get a string back.  For example,

     

    Code Block

    for (int i = 0; i < 60; i++)

    {

    Console.WriteLine(ConvertDecString(i));

    }

     

     

     

    Would output

     

    A

    B

    C

    ...

    Z

    AA

    BA

    BB

    BC

    ...

     

    It converts the number into base26 and then substitutes a character for the number.

     

    Hope this helps.

     

    Thursday, October 18, 2007 10:19 AM

All replies

  • Hi

     

    try this code

     

    private void button_Click(object sender, EventArgs e)

    {

    bool flag=false ;

    for (int i = 65; i <=90; i++)

    {

    if (flag)

    {

    for (int k = 65; k <= 90; k++)

    {

    for (int j = 65; j <= 90; j++)

    {

    char tt = (char)k;

    char ttt = (char)j;

    string result = tt.ToString() + ttt.ToString() ;

    textBox1.Text = textBox1.Text + " -" + result;

    }

    i = 90;

    }

    }

    else

    {

    char tt = (char)i;

    textBox1.Text = textBox1.Text + "- " + tt;

    if (i == 90)

    {

    flag = true;

    i -= 1;

    }

    }

    }

    }

     

      As above this first loop using to display letter from a-to z   other two looping using to display like aa-ab ba   ----etc

     

    Regards

    palan

    Thursday, October 18, 2007 2:33 AM
  • Very nice - I've tried using the for and while loop commands but not like this - thank you i should be able to implement this script easly into my program.

     

    Osiris

     

    EDIT: I'm sorry - I realized afterward that what I truely need is a script that can do what your script can do but continue past two digits. ex a... ab... abc... and so on. I am tryin to modd it right now and got something working but its so slow. If you know a faster way please help.

     

    Osiris

    Thursday, October 18, 2007 3:36 AM
  • Perhaps this will help?

     

    Code Block

    public string ConvertDecString(int value)

    {

    string CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    string retVal = string.Empty;

    double value = Convert.ToDouble(dvalue);

     

    do

    {

    double remainder = value - (26 * Math.Truncate(value / 26));

    retVal = retVal + CHARS.Substring((int)remainder, 1);

    value = Math.Truncate(value / 26);

    } while (value > 0);

     

    return retVal;

    }

     

     

     

    To use it, just pass it a value and you will get a string back.  For example,

     

    Code Block

    for (int i = 0; i < 60; i++)

    {

    Console.WriteLine(ConvertDecString(i));

    }

     

     

     

    Would output

     

    A

    B

    C

    ...

    Z

    AA

    BA

    BB

    BC

    ...

     

    It converts the number into base26 and then substitutes a character for the number.

     

    Hope this helps.

     

    Thursday, October 18, 2007 10:19 AM
  • Or if you want something to really 'increment' letters rather than having to convert back and forth to numbers, here is a simple recursive app:

    Code Block

    class Class1

    {

    [STAThread]

    static void Main(string[] args)

    {

    string s = "a";

    while (s.Length < 5) // for this demonstration we'll just go up to 'zzzz'

    {

    Console.WriteLine(s);

    s = Increment(s);

    }

    }

     

    static string Increment(string s)

    {

    // first case - string is empty: return "a"

    if ((s == null) || (s.Length == 0))

    return "a";

    // next case - last char is less than 'z': simply increment last char

    char lastChar = s[s.Length - 1];

    string fragment = s.Substring(0, s.Length - 1);

    if (lastChar < 'z')

    {

    ++lastChar;

    return fragment + lastChar;

    }

    // next case - last char is 'z': roll over and increment preceding string

    return Increment(fragment) + 'a';

    }

    }

     

    Thursday, October 18, 2007 2:36 PM
  • Hello, Awesome program! Does anyone know if eradicator's program can be tweaked to also increment numbers and skip letters that begin or end with I, O, or Q? Thanks!
    Saturday, June 21, 2008 12:41 AM
  • dear A Million Lemmings,
    code you gave here is realy working well . thx for it. the problem with that code is that it is missing the value like AA,AB,AC ,AD ,AE ....AZ,AAA,AAB......
    i modified your code bit and it seems working well.i am running out of time so i  checked it one or two time.
    if there is any mistake in my code kindly inform me too. 


    private string ConvertDecString(int value)

    {

    string CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    string retVal = string.Empty;

    int remainder=0;

    do

    {

    remainder = value % 26;

    retVal = CHARS.Substring(remainder, 1) + retVal;

    value = (value / 26)-1;



    } while ((value+1) > 0);



    return retVal;

    }



    Regards
    Sameeullah Daris

    Thursday, March 26, 2009 10:09 AM
  • We thank you. Works great.


    public static string ConvertDecString(this int value, bool toLower) {
    	string retVal = String.Empty;
    	if ((value - 1) > 0) {
    
    	string CHARS = "ABCDEFGHIJKLMNOPQRSTUVWYZŹŻ";
    //"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    	int remainder=0;
    	do {
    		remainder = value % CHARS.Length;
    		retVal = CHARS.Substring(remainder, 1) + retVal;
    		value = (value / CHARS.Length)-1;
    	} while ((value+1) > 0);
    
    	if (toLower)
    		retVal = retVal.ToLower();
            }
            else {
    	retVal = "A";
    	if (toLower)
    		retVal = retVal.ToLower();
    	}
          
          return retVal;
    }

    Inverse operation, could look like.


    		public static int ConvertStringDec(this string letter) {
    			if (letter!=null) {
    				if (letter.Length>0) {
    					string CHARS = "ABCDEFGHIJKLMNOPQRSTUVWYZŹŻ";
    					if (letter.Length == 1) {
    						for (int i = 0; i < CHARS.Length; i++) {
    							if (CHARS[i].ToString().ToLower() == letter.ToLower().Trim()) {
    								return i+1;
    							}
    						}
    					}
    					else {
    						int k = 0;
    
    						for (int d = 0; d < letter.Length-1; d++) {
    							k += CHARS.Length-1;
    						}
    
    
    						for (int i = 0; i < CHARS.Length; i++) {
    						 if (CHARS[i].ToString().ToLower() == letter[letter.Length-1].ToString().ToLower().Trim()) {
    								k += i+1;
    							}
    						}
    
    
    						return k;
    					}
    				}
    			}
    			
    			return 1;
    		}


    Regards
    Krzysztof Radzimski
    Sunday, January 10, 2010 5:51 PM