473,592 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to increment a letter?

Given the following declaration:

char letter = 'A';

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

Thanks.

Mansi
Nov 16 '05 #1
16 21973
Hi,

How about writting a method

private char Increment(char c)
{
int i = ascii of 'c';
if (i<maxascii)
i ++;
return asciiconverttoc har(i);
}

Nirosh.

"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:B8******** *************** ***********@mic rosoft.com...
Given the following declaration:

char letter = 'A';

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

Thanks.

Mansi

Nov 16 '05 #2
Or simply this -- (char)((int)let ter + 1) ;

Rule - always avoid calling a method for performance reasons. Calling a
method means push/pop of all local stack variables - very painful for the
CPU.

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:B8******** *************** ***********@mic rosoft.com...
Given the following declaration:

char letter = 'A';

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

Thanks.

Mansi

Nov 16 '05 #3

"Sahil Malik" <co************ *****@nospam.co m> a écrit dans le message de
news: ef************* *@TK2MSFTNGP11. phx.gbl...
Or simply this -- (char)((int)let ter + 1) ;

Rule - always avoid calling a method for performance reasons. Calling a
method means push/pop of all local stack variables - very painful for the
CPU.
This rule is **very** debatable. I would propose instead:

Rule 1: Always introduce a method when you have identified a piece of code
that could be reused instead of duplicating the code everywhere, especially
if the method body is a bit obscure (like the double cast that you have to
do to get the next letter).

Rule 2: If you identify a performance bottleneck with a profiler, try to
inline the method and see if it makes a difference (if it does not, keep the
method, the problem is elsewhere).

Also, calling a method does not "push/pop all local stack variables" (which
leads to think that all the variables are copied in the process), it pushes
and pops a "stack frame", which is a very simple and very fast operation.
Inlining code often leads to less efficient code, simply because the code
gets bigger and you start to get trashing in the CPU cache.

Bruno.

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:B8******** *************** ***********@mic rosoft.com...
Given the following declaration:

char letter = 'A';

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

Thanks.

Mansi


Nov 16 '05 #4
Malik,

what if the letter = 'z' or 'Z'??

Nirosh.

"Sahil Malik" <co************ *****@nospam.co m> wrote in message
news:ef******** ******@TK2MSFTN GP11.phx.gbl...
Or simply this -- (char)((int)let ter + 1) ;

Rule - always avoid calling a method for performance reasons. Calling a
method means push/pop of all local stack variables - very painful for the
CPU.

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:B8******** *************** ***********@mic rosoft.com...
Given the following declaration:

char letter = 'A';

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

Thanks.

Mansi


Nov 16 '05 #5
Okay educate me on this --

I thought "calling a method does not "push/pop all local stack
variables"" --- and I learnt this in Turbo C days. What is a stack frame? It
is quite probable that modern day compilers use an alternate technique.

Until today, maybe, I was a big fan of inlining code.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


"Bruno Jouhier [MVP]" <bj******@clu b-internet.fr> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...

"Sahil Malik" <co************ *****@nospam.co m> a écrit dans le message de
news: ef************* *@TK2MSFTNGP11. phx.gbl...
Or simply this -- (char)((int)let ter + 1) ;

Rule - always avoid calling a method for performance reasons. Calling a
method means push/pop of all local stack variables - very painful for the
CPU.


This rule is **very** debatable. I would propose instead:

Rule 1: Always introduce a method when you have identified a piece of code
that could be reused instead of duplicating the code everywhere,
especially if the method body is a bit obscure (like the double cast that
you have to do to get the next letter).

Rule 2: If you identify a performance bottleneck with a profiler, try to
inline the method and see if it makes a difference (if it does not, keep
the method, the problem is elsewhere).

Also, calling a method does not "push/pop all local stack variables"
(which leads to think that all the variables are copied in the process),
it pushes and pops a "stack frame", which is a very simple and very fast
operation. Inlining code often leads to less efficient code, simply
because the code gets bigger and you start to get trashing in the CPU
cache.

Bruno.

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:B8******** *************** ***********@mic rosoft.com...
Given the following declaration:

char letter = 'A';

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

Thanks.

Mansi



Nov 16 '05 #6
Then it goes to the next ascii character.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"Champika Nirosh" <te**@test.lk > wrote in message
news:OR******** ******@TK2MSFTN GP09.phx.gbl...
Malik,

what if the letter = 'z' or 'Z'??

Nirosh.

"Sahil Malik" <co************ *****@nospam.co m> wrote in message
news:ef******** ******@TK2MSFTN GP11.phx.gbl...
Or simply this -- (char)((int)let ter + 1) ;

Rule - always avoid calling a method for performance reasons. Calling a
method means push/pop of all local stack variables - very painful for the
CPU.

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:B8******** *************** ***********@mic rosoft.com...
> Given the following declaration:
>
> char letter = 'A';
>
> Is there a way that I can increment letter so that 'B' is returned?
>
> Thanks.
>
> Mansi
>
>



Nov 16 '05 #7
which mean your line of code gives a wrong answer since he is looking for an
increament for char in between a-z, A-Z (at least as I got)

So if you are to write an inline code still, I think you got to change your
answer...

Nirosh.

"Sahil Malik" <co************ *****@nospam.co m> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Then it goes to the next ascii character.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"Champika Nirosh" <te**@test.lk > wrote in message
news:OR******** ******@TK2MSFTN GP09.phx.gbl...
Malik,

what if the letter = 'z' or 'Z'??

Nirosh.

"Sahil Malik" <co************ *****@nospam.co m> wrote in message
news:ef******** ******@TK2MSFTN GP11.phx.gbl...
Or simply this -- (char)((int)let ter + 1) ;

Rule - always avoid calling a method for performance reasons. Calling a
method means push/pop of all local stack variables - very painful for the CPU.

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:B8******** *************** ***********@mic rosoft.com...
> Given the following declaration:
>
> char letter = 'A';
>
> Is there a way that I can increment letter so that 'B' is returned?
>
> Thanks.
>
> Mansi
>
>



Nov 16 '05 #8
The details depend on the calling conventions of the language (C and Pascal
have different conventions). But the typical x86 instruction flow looks
like:

PUSH ebp
MOV ebp, esp
SUB esp, 10 (replace 10 by the stack space you need for your local
variables).
...
POP ebp
RET

These instructions only take a few clock cycles (6 cycles for the PUSH, MOV,
SUB prologue on a 386). So, this is really fast.

The local variables of the caller don't need to copied, the ebp register
gives the base address for the locals, and it just needs to be pushed and
popped when the method is entered / existed (this is called pushing and
popping a stack frame). On the other hand, the registers must be saved if
they would be clobbered by the callee (but there is a fast instruction to do
this on modern processors).

Also, when you compile your .NET code in "optimized" mode, the compiler (or
the jitter) will inline some methods for you (as long as they are not
virtual/overridable). The jitter can do a much better job than you in
deciding what should be inlined and what should not because it knows the
details of the processor architecture. So, let it do the work for you!

Note: I am not at all an assembly language expert (it has been so long...),
so what I say above may need some verifications, but the general idea should
be right.

Bruno.

"Sahil Malik" <co************ *****@nospam.co m> a écrit dans le message de
news: uS************* *@TK2MSFTNGP10. phx.gbl...
Okay educate me on this --

I thought "calling a method does not "push/pop all local stack
variables"" --- and I learnt this in Turbo C days. What is a stack frame?
It is quite probable that modern day compilers use an alternate technique.

Until today, maybe, I was a big fan of inlining code.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


"Bruno Jouhier [MVP]" <bj******@clu b-internet.fr> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...

"Sahil Malik" <co************ *****@nospam.co m> a écrit dans le message de
news: ef************* *@TK2MSFTNGP11. phx.gbl...
Or simply this -- (char)((int)let ter + 1) ;

Rule - always avoid calling a method for performance reasons. Calling a
method means push/pop of all local stack variables - very painful for
the CPU.


This rule is **very** debatable. I would propose instead:

Rule 1: Always introduce a method when you have identified a piece of
code that could be reused instead of duplicating the code everywhere,
especially if the method body is a bit obscure (like the double cast that
you have to do to get the next letter).

Rule 2: If you identify a performance bottleneck with a profiler, try to
inline the method and see if it makes a difference (if it does not, keep
the method, the problem is elsewhere).

Also, calling a method does not "push/pop all local stack variables"
(which leads to think that all the variables are copied in the process),
it pushes and pops a "stack frame", which is a very simple and very fast
operation. Inlining code often leads to less efficient code, simply
because the code gets bigger and you start to get trashing in the CPU
cache.

Bruno.

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:B8******** *************** ***********@mic rosoft.com...
Given the following declaration:

char letter = 'A';

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

Thanks.

Mansi



Nov 16 '05 #9

"Sahil Malik" <co************ *****@nospam.co m> a écrit dans le message de
news: %2************* ***@TK2MSFTNGP1 0.phx.gbl...
Then it goes to the next ascii character.
Actually, the next UNICODE character (ASCII stops at 127).

Another reason to package it as a method:

char NextUnicode(cha r ch) { return (char)((int)ch + 1); }

char NextLetter(char ch)
{
Assert(Characte r.IsLetter(ch)) ;
char nextCh = (char)((int)ch + 1);
if (!Character.IsL etter(ch))
throw exception or return special value to indicate overflow;
return nextCh;
}

If you decide to throw an exception in debug mode, and still want fast
production code, you can use conditional compilation to eliminate the Assert
and the if test in release mode.

Bruno.


- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"Champika Nirosh" <te**@test.lk > wrote in message
news:OR******** ******@TK2MSFTN GP09.phx.gbl...
Malik,

what if the letter = 'z' or 'Z'??

Nirosh.

"Sahil Malik" <co************ *****@nospam.co m> wrote in message
news:ef******** ******@TK2MSFTN GP11.phx.gbl...
Or simply this -- (char)((int)let ter + 1) ;

Rule - always avoid calling a method for performance reasons. Calling a
method means push/pop of all local stack variables - very painful for
the
CPU.

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Mansi" <Ma***@discussi ons.microsoft.c om> wrote in message
news:B8******** *************** ***********@mic rosoft.com...
> Given the following declaration:
>
> char letter = 'A';
>
> Is there a way that I can increment letter so that 'B' is returned?
>
> Thanks.
>
> Mansi
>
>



Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
6013
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
6860
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>
8
21680
by: Mansi | last post by:
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.
3
2736
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
11060
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
2794
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
3888
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
1423
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
3727
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
7871
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7995
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
6642
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5400
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3851
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
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1202
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.