How to increment a letter?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Given the following declaration:

char letter = 'A';

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

Thanks.

Mansi
 
Hi,

How about writting a method

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

Nirosh.
 
Sahil Malik said:
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.
 
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
 
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.
 
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 said:
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 said:
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
 
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;
}
 
Chris R. Timmons said:
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...
 
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.
 
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.
 
Thanks for all the responses.

Mansi

Champika Nirosh said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top