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
2009-06-22 09:17:40
what does that return for 'z'?
Colin Pickard
2009-06-22 09:18:56
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
2009-06-22 09:21:37
if first char = 'z', this code will give the next char which is '{', thus not in the alphabet.
rogeriopvl
2009-06-22 09:22:56
No, let him find out by himself why it doesn't work ;-)
Treb
2009-06-22 09:22:57
nah, enough variation and discussion of answers will give the asker the desired exercise of thinking the question through
Colin Pickard
2009-06-22 09:23:42
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
2009-06-22 09:25:29
Well, he accepted the answer, so it probably works for him :)
Daniel Daranas
2009-06-22 09:28:34
Let's hope you're not using EBDIC encoding.
Skizz
2009-06-22 09:38:13
+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
2009-06-22 09:18:25
Of course, this might not be what the homework question is getting at...
Colin Pickard
2009-06-22 09:25:29
+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
2009-06-22 09:19:01
A:
need to just add 1 in character you get next character. It works on ASCII values.
Nakul Chaudhary
2009-06-22 09:19:43
+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
2009-06-22 09:28:36
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
2009-09-03 10:03:35