tags:

views:

631

answers:

6

How we can find out the next character of the entered one. For example, if I entered the character "b" then how do I get the answer "c"?

+3  A: 

How about:

char first = 'c';
char nextChar = (char)((int) first + 1);
Hemant
what does that return for 'z'?
Colin Pickard
That will surely NOT work. I was trying to be simplistic by not adding validation code. But seeing the comments to question, I think I shouldn't have answered. Should I delete my answer?
Hemant
if first char = 'z', this code will give the next char which is '{', thus not in the alphabet.
rogeriopvl
No, let him find out by himself why it doesn't work ;-)
Treb
nah, enough variation and discussion of answers will give the asker the desired exercise of thinking the question through
Colin Pickard
IT returns the next value from ascii table (http://www.asciitable.com/) . Of course you could also add a simple: if (nextchar > 'z') nextChar = 'a'; (adding more logic (ie Capitalized letters) is also very simple)
Rekreativc
Well, he accepted the answer, so it probably works for him :)
Daniel Daranas
Let's hope you're not using EBDIC encoding.
Skizz
+2  A: 

Perhaps the simplest way is a little function and an array of the 26 chars. Then you can decide what you want to return for 'z'.

Colin Pickard
Of course, this might not be what the homework question is getting at...
Colin Pickard
+1  A: 

Convert the character to a number, increment the number and then convert back.

But consider what will happen for "z" or "á" (Latin Small Leter A with Acute).

Richard
A: 

need to just add 1 in character you get next character. It works on ASCII values.

Nakul Chaudhary
... up to 'y' .
Colin Pickard
+5  A: 

Try this:

char letter = 'c';

if (letter == 'z')
    nextChar = 'a';
else if (letter == 'Z')
    nextChar = 'A';

else
    nextChar = (char)(((integer) letter) + 1);

This way you have no trouble when the char is the last of the alphabet.

rogeriopvl
Wait - replace the '+ 1' with '+ 13' and you get...?
Treb
A: 

How does ä sort? In German (I think) it should sort after a, but in Swedish it should come after å, which in turn is after z. This is not a trivial question, unless you restrict yourself to English.

erikkallen