473,785 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 21707
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="ABCDE FGHIJKLMNOPQRST UVWXYZ";

public char NextLetter(char letter)
{
return alphabet[alphabet.IndexO f(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)le tter + 1);

"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:82******** *************** ***********@mic rosoft.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)le tter + 1);

"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:82******** *************** ***********@mic rosoft.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.mi crosoft.com> wrote in
message news:21******** *************** ***********@mic rosoft.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)le tter + 1);

"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:82******** *************** ***********@mic rosoft.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.IndexO f(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
6034
by: Alan J. Flavell | last post by:
OK, today's email brought a comment from a reader, pointing out something that I'd long since noticed myself but hadn't done anything about it. On my pages, I've got a first-letter style on paragraphs. So far, so good: some of the paragraphs begin with a link, and there are styles in the stylesheet for link, hover etc. The first letter is still displayed as styled for the paragraph's first-letter.
1
6877
by: Patrick | last post by:
I am trying to get "first-letter" to work inline withing an anchor. Actually I have not been able to get it to work within an <a> tag whether inline, linked, or embedded. Using IE6 service pack 1. <html> <head> <style type="text/css"> h5 {background-color: red; padding-left: 5px} a {color: white; font-size: 20px; text-decoration: none} </style>
16
21989
by: Mansi | last post by:
Given the following declaration: char letter = 'A'; Is there a way that I can increment letter so that 'B' is returned? Thanks. Mansi
3
2746
by: George Ter-Saakov | last post by:
What is the purpose of having Interlocked.Increment if it does not work with variable declared as volatile. Here is my problem, Interlocked.Increment increments the variable in thread safe manner. But at the same time if i want to use variable that could be changed in another thread i must use volatile (to prevent optimization). But then i can not use Interlocked.Increment. So i do not see any benefits of having ...
3
11072
by: questionit | last post by:
Hi Experts how to implement this in Java . example problem: 1) Input string e.g "HAL" 2) Output IBM ( each letter in string replaced with next character whatever comes in ABC... Thanks
3
2890
by: nfrjob | last post by:
Hi there, I'm an advanced beginner, so please bear with me... I have a simple table that has four fields in it: Zone (which will store a three letter code such as ABC), Year (a four digit number field), Number (which is the problem field) and Label (which will be the concatenation of the previous three fields). There will be a variety of three letter codes stored in the Zone field (11 to be precise) and the Year field will have a very...
11
3913
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
3
1434
by: Wayne L | last post by:
I posted a question simalar to this before but was not clear on what I was trying to do. I am using two tables one primary and a temp table. I import my data to the temp table and bounce it against the primary table and append the primary table with the new data for export. The problem is I have set a command button to do this and if the user selects the button twice the name of the output.txt file is then zero. I need to add an increment number...
13
3739
by: umpsumps | last post by:
Hello, Here is my code for a letter frequency counter. It seems bloated to me and any suggestions of what would be a better way (keep in my mind I'm a beginner) would be greatly appreciated.. def valsort(x): res = for key, value in x.items(): res.append((value, key))
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.