Click here to Skip to main content
11,642,381 members (66,611 online)
Click here to Skip to main content

Count with Letters

, 16 Jun 2007 CPOL 27.1K 152 11
Rate this:
Please Sign up or sign in to vote.
A way to use letters instead of numbers to count

Introduction

I had to write a "text tool" application a few days ago, and one of the requests was to "number" text lines with letters (like Microsoft Word does from bullets and numbering). Because I couldn't find an "already made" function for this, I had to do it myself. It's not difficult - this article is for beginner's (plus) level.

Background

To understand this function, all you need is to have basic knowledge of C# (to know the char type, the string type and to understand how to convert between these types).

Using the Code

This code is free. You can use and redistribute. You can improve it (because it is not perfect).

The code

The function is like this:

private string GetNext(string current) 
{return curent + 1; // the next value}

Let's say that current = "a", then, the return will be "b". If the current = "z", the return will be "aa". If current = "abcd" the return is "abce" and so on.

To do this, I need 2 auxiliary functions:

private char GetNextChar(char c)
{
	if (c < 'z')
		return (char)((int)c + 1);
	else 
		return 'a'; 
} 

and:

private string ReverseString(string str)
{
	StringBuilder sb = new StringBuilder();
	for (int i = str.Length - 1; i >= 0; i--)
		sb.Append(str[i]);
	return sb.ToString();
}

The ReverseString function is only because I feel more comfortable to work with the string from left to right.

The main function is:

private string GetNext(string curent)
{
	string next = "";
	int depl = 0;
	curent = ReverseString(curent);
	char curent_digit;

	curent_digit = GetNextChar(curent[0]);
	if (curent_digit < curent[0])
		depl = 1;
	else
		depl = 0;
	next = curent_digit.ToString();
            
	for (int i = 1; i < curent.Length; i++)
	{
		curent_digit = curent[i];
		if (depl != 0)
		{
			curent_digit = GetNextChar(curent[i]);
			if (curent_digit < curent[i])
				depl = 1;
			else
				depl = 0;
		}
	                
		next += curent_digit.ToString();
	
	}
	if (depl == 1)
		next += 'a'.ToString();
	return ReverseString(next);
} 

As an example of usage for this:

private void btnNext_Click(object sender, EventArgs e)
{
	string s = txtCurent.Text;
	StringBuilder tmp = new StringBuilder();
	for (int i = 0; i < 10000; i++)
	{
		s = GetNext(s);
		tmp.AppendLine(s);
	}
	lblLast.Text = s.ToString();
}

A form, with a button and a textbox (with multiline = true) to see the result.

History

  • 17th June, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

zeltera
Engineer
Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Michael Ascker20-Jan-11 12:26
memberMichael Ascker20-Jan-11 12:26 
General[My vote of 2] My vote of 2 Pin
Rajkumar-Kannan28-Feb-10 16:45
memberRajkumar-Kannan28-Feb-10 16:45 
GeneralRe: [My vote of 2] My vote of 2 Pin
zeltera28-Feb-10 18:50
memberzeltera28-Feb-10 18:50 
GeneralMy vote of 1 Pin
BillWoodruff26-Dec-09 18:28
memberBillWoodruff26-Dec-09 18:28 
GeneralLetter arithmetic Pin
djlove21-Jun-07 6:23
memberdjlove21-Jun-07 6:23 
GeneralPosible extension for this application Pin
zeltera19-Jun-07 23:25
memberzeltera19-Jun-07 23:25 
GeneralString + Integer Pin
Steve Hansen18-Jun-07 3:11
memberSteve Hansen18-Jun-07 3:11 
GeneralRe: String + Integer Pin
zeltera18-Jun-07 3:21
memberzeltera18-Jun-07 3:21 
GeneralRe: String + Integer Pin
zeltera18-Jun-07 3:30
memberzeltera18-Jun-07 3:30 
GeneralRe: String + Integer Pin
Steve Hansen18-Jun-07 5:22
memberSteve Hansen18-Jun-07 5:22 
GeneralRe: String + Integer Pin
zeltera18-Jun-07 6:13
memberzeltera18-Jun-07 6:13 
GeneralActually... Pin
sharpiesharpie17-Jun-07 1:00
membersharpiesharpie17-Jun-07 1:00 
GeneralRe: Actually... Pin
zeltera17-Jun-07 1:12
memberzeltera17-Jun-07 1:12 
GeneralRe: Actually... [modified] Pin
sharpiesharpie17-Jun-07 1:56
membersharpiesharpie17-Jun-07 1:56 
GeneralRe: Actually... Pin
Kamarey17-Jun-07 10:04
memberKamarey17-Jun-07 10:04 
GeneralRe: Actually... Pin
zeltera17-Jun-07 10:38
memberzeltera17-Jun-07 10:38 
GeneralRe: Actually... Pin
Kamarey19-Jun-07 5:17
memberKamarey19-Jun-07 5:17 
GeneralRe: Actually... Pin
djlove19-Jun-07 23:15
memberdjlove19-Jun-07 23:15 
GeneralRe: Actually... Pin
Kamarey20-Jun-07 2:19
memberKamarey20-Jun-07 2:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 17 Jun 2007
Article Copyright 2007 by zeltera
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid