469,645 Members | 1,551 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 21677
Hi,

How about writting a method

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

Nirosh.

"Mansi" <Ma***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.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)letter + 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***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.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.com> a écrit dans le message de
news: ef**************@TK2MSFTNGP11.phx.gbl...
Or simply this -- (char)((int)letter + 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***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.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.com> wrote in message
news:ef**************@TK2MSFTNGP11.phx.gbl...
Or simply this -- (char)((int)letter + 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***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.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******@club-internet.fr> wrote in message
news:uh**************@TK2MSFTNGP12.phx.gbl...

"Sahil Malik" <co*****************@nospam.com> a écrit dans le message de
news: ef**************@TK2MSFTNGP11.phx.gbl...
Or simply this -- (char)((int)letter + 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***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.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**************@TK2MSFTNGP09.phx.gbl...
Malik,

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

Nirosh.

"Sahil Malik" <co*****************@nospam.com> wrote in message
news:ef**************@TK2MSFTNGP11.phx.gbl...
Or simply this -- (char)((int)letter + 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***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.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.com> wrote in message
news:%2****************@TK2MSFTNGP10.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**************@TK2MSFTNGP09.phx.gbl...
Malik,

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

Nirosh.

"Sahil Malik" <co*****************@nospam.com> wrote in message
news:ef**************@TK2MSFTNGP11.phx.gbl...
Or simply this -- (char)((int)letter + 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***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.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.com> 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******@club-internet.fr> wrote in message
news:uh**************@TK2MSFTNGP12.phx.gbl...

"Sahil Malik" <co*****************@nospam.com> a écrit dans le message de
news: ef**************@TK2MSFTNGP11.phx.gbl...
Or simply this -- (char)((int)letter + 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***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.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.com> a écrit dans le message de
news: %2****************@TK2MSFTNGP10.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(char ch) { return (char)((int)ch + 1); }

char NextLetter(char ch)
{
Assert(Character.IsLetter(ch));
char nextCh = (char)((int)ch + 1);
if (!Character.IsLetter(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**************@TK2MSFTNGP09.phx.gbl...
Malik,

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

Nirosh.

"Sahil Malik" <co*****************@nospam.com> wrote in message
news:ef**************@TK2MSFTNGP11.phx.gbl...
Or simply this -- (char)((int)letter + 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***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.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
"Sahil Malik" <co*****************@nospam.com> wrote in
news:ef**************@TK2MSFTNGP11.phx.gbl...
Or simply this -- (char)((int)letter + 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.


Heavens, no!

1: The JIT will inline small functions like these anyway, so at worst,
there's no difference (well, only in source code readability).
2: The JIT optimizer usually does a better job on smaller functions (at
least the current version) - it can only enregister one variable per
register per function, so if you combine complex functions, chances are it
will enregister less variables, which means a performance loss.
3: Expanding functions manually will usually make the code bigger, which
increases the chances for a cache miss.
4: Never trust rules like these without doing your own benchmark.

Niki
Nov 16 '05 #11
"=?Utf-8?B?TWFuc2k=?=" <Ma***@discussions.microsoft.com> wrote in
news:B8**********************************@microsof t.com:
Given the following declaration:

char letter = 'A';

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


Mansi,

System.Char is implemented as a 16-bit number, so you can simply do
this:

public char GetNextLetter(char letter)
{
return ++letter;
}

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #12
Chris R. Timmons <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote:
System.Char is implemented as a 16-bit number, so you can simply do
this:

public char GetNextLetter(char letter)
{
return ++letter;
}


Note that this is identical to the slightly simpler (IMO):

public char GetNextLetter (char letter)
{
return letter+1;
}

(No need for the implicit assignment.)

Then again, if you're using the simple Unicode increment, I probably
wouldn't bother putting that in a method...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
Fantastic, .. I wish there was an article I could read about brand new
compiler optimizations & basic changes in these new fangled CPUs. So does
this mean Recursive functions aren't as bad as they are made out to be?

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message
news:uV**************@TK2MSFTNGP10.phx.gbl...
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.com> 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******@club-internet.fr> wrote in message
news:uh**************@TK2MSFTNGP12.phx.gbl...

"Sahil Malik" <co*****************@nospam.com> a écrit dans le message de news: ef**************@TK2MSFTNGP11.phx.gbl...
Or simply this -- (char)((int)letter + 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***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.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 #14
They still run the possibility of using up all the stack and creating
an exception, and in general are still not going to perform as well as
an iterative solution. They sure do look eloquent though.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 21 Oct 2004 11:44:24 -0400, "Sahil Malik"
<co*****************@nospam.com> wrote:
Fantastic, .. I wish there was an article I could read about brand new
compiler optimizations & basic changes in these new fangled CPUs. So does
this mean Recursive functions aren't as bad as they are made out to be?

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message
news:uV**************@TK2MSFTNGP10.phx.gbl...
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.com> 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 stackframe? > It is quite probable that modern day compilers use an alternatetechnique. >
> Until today, maybe, I was a big fan of inlining code.
>
> - Sahil Malik
> http://dotnetjunkies.com/weblog/sahilmalik
>
>
>
>
> "Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message
> news:uh**************@TK2MSFTNGP12.phx.gbl...
>>
>> "Sahil Malik" <co*****************@nospam.com> a écrit dans le messagede >> news: ef**************@TK2MSFTNGP11.phx.gbl...
>>> Or simply this -- (char)((int)letter + 1) ;
>>>
>>> Rule - always avoid calling a method for performance reasons. Callinga >>> 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 castthat >> you have to do to get the next letter).
>>
>> Rule 2: If you identify a performance bottleneck with a profiler, tryto >> 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 theprocess), >> it pushes and pops a "stack frame", which is a very simple and veryfast >> 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***@discussions.microsoft.com> wrote in message
>>> news:B8**********************************@microsof t.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 #15
Hi Jon, Chris,

Yes this is easy

But at any rate I don't think that he is expecting 'z' to return '{' or 'Z'
to return '['.

Nirosh.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Chris R. Timmons <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote:
System.Char is implemented as a 16-bit number, so you can simply do
this:

public char GetNextLetter(char letter)
{
return ++letter;
}


Note that this is identical to the slightly simpler (IMO):

public char GetNextLetter (char letter)
{
return letter+1;
}

(No need for the implicit assignment.)

Then again, if you're using the simple Unicode increment, I probably
wouldn't bother putting that in a method...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #16
Thanks for all the responses.

Mansi

"Champika Nirosh" wrote:
Hi Jon, Chris,

Yes this is easy

But at any rate I don't think that he is expecting 'z' to return '{' or 'Z'
to return '['.

Nirosh.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Chris R. Timmons <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote:
System.Char is implemented as a 16-bit number, so you can simply do
this:

public char GetNextLetter(char letter)
{
return ++letter;
}


Note that this is identical to the slightly simpler (IMO):

public char GetNextLetter (char letter)
{
return letter+1;
}

(No need for the implicit assignment.)

Then again, if you're using the simple Unicode increment, I probably
wouldn't bother putting that in a method...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #17

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
8 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.