469,934 Members | 1,829 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

How to increment letter

Given the following declaration:

String letter = "A";

Is there a way that I can increment letter so that "B" is returned?

Is there a way that I can add an offset so that let's say, "G" is returned?

Thanks.

Mansi
Nov 16 '05 #1
8 20831
That all depends on exactly what you want.

In the old ASCII days, the algorithm was easy: just add 1 to the 8 bits
that make up the character, and if it reaches, say Z, reset it to A. (I
know that .NET doesn't allow you to treat characters like integers, but
bear with me.)

In the new world of Unicode, it's not entirely clear what you mean by
"increment". For example, if you're in Germany, they have a letter that
looks like a fancy "B" that represents "ss" (I think it's called the
s-set, or something like that). Where does this occur in the sort
order? When you're incrementing through the alphabet in Germany, I
assume that this letter has to show up at some point. Where?

What about in Turkey? They have letters with accents and other glyphs
in their alphabet. Where do they fall in the sequence?

In my brief travels through .NET's globalization classes, I remember
seeing information on alphabetic sorting and sequencing, so there is
probably a way to iterate through the alphabet given a cultural
context.

My point is that if you really want to be able to iterate through the
alphabet, in any culture that happens to be set on the PC running your
program, you have to bring a lot of .NET machinery to bear in order to
do it.

Or, from the opposite point of view, if you "just want to iterate
through the alphabet", you can't expect .NET to supply that for you
very easily, because it has to take every world culture into account.

So, back to your problem, this is where I usually just throw up my
hands and make a string:

const string alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public char NextLetter(char letter)
{
return alphabet[alphabet.IndexOf(letter) % alphabet.Length];
}

Not very elegant... but as I pointed out, if you want elegant then you
have a long road ahead of you. :)

Nov 16 '05 #2
Maybe you can try something like

char letter = 'A';

letter = (char)((byte)letter + 1);

"Mansi" <Ma***@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
Given the following declaration:

String letter = "A";

Is there a way that I can increment letter so that "B" is returned?

Is there a way that I can add an offset so that let's say, "G" is returned?
Thanks.

Mansi

Nov 16 '05 #3
If you like it simple yo could do:

char letter = 'A';
a++;

"Peter Rilling" wrote:
Maybe you can try something like

char letter = 'A';

letter = (char)((byte)letter + 1);

"Mansi" <Ma***@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
Given the following declaration:

String letter = "A";

Is there a way that I can increment letter so that "B" is returned?

Is there a way that I can add an offset so that let's say, "G" is

returned?

Thanks.

Mansi


Nov 16 '05 #4
Oh. Cool.

"Marinus Holkema" <Ma************@discussions.microsoft.com> wrote in
message news:21**********************************@microsof t.com...
If you like it simple yo could do:

char letter = 'A';
a++;

"Peter Rilling" wrote:
Maybe you can try something like

char letter = 'A';

letter = (char)((byte)letter + 1);

"Mansi" <Ma***@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
Given the following declaration:

String letter = "A";

Is there a way that I can increment letter so that "B" is returned?

Is there a way that I can add an offset so that let's say, "G" is

returned?

Thanks.

Mansi


Nov 16 '05 #5
char letter = 'a';
a++; //increment
a += (char)2; //offset

"Mansi" wrote:
Given the following declaration:

String letter = "A";

Is there a way that I can increment letter so that "B" is returned?

Is there a way that I can add an offset so that let's say, "G" is returned?

Thanks.

Mansi

Nov 16 '05 #6
Hmm declare a variable as "letter" and go on with 'a'. I should not answer
questions before coffee. I hope the meaning is clear.

"Marinus Holkema" wrote:
char letter = 'a';
a++; //increment
a += (char)2; //offset

"Mansi" wrote:
Given the following declaration:

String letter = "A";

Is there a way that I can increment letter so that "B" is returned?

Is there a way that I can add an offset so that let's say, "G" is returned?

Thanks.

Mansi

Nov 16 '05 #7
Apparently I shouldn't write long, rambling responses late at night,
either. :)

Nov 16 '05 #8
Bruce.... Actually I think it is good to post algorithms like this. No
need to
reinvent the wheel.

Regards,
Jeff
public char NextLetter(char letter)
{
return alphabet[alphabet.IndexOf(letter) % alphabet.Length];
}
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Alan J. Flavell | last post: by
1 post views Thread by Patrick | last post: by
16 posts views Thread by Mansi | last post: by
3 posts views Thread by George Ter-Saakov | last post: by
3 posts views Thread by questionit | last post: by
11 posts views Thread by divya_rathore_ | last post: by
13 posts views Thread by umpsumps | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.