I'm having issues figuring out how to get my code to increment the string that is given by user input so that when a user chooses to replace a letter like z it would go to a, b to c etc. The catch is I have to do this without using boolean. I am supposed to get this by using arithmetics to get the promotion from z to a from the users input. Plus must be only lower case letters from a-z. Any help would be appreciated thanks.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
This piece of code
will output
What it does is take the character, substract the character code for 'a', thus given a value from 0 to 25. Then we increment 1. Take that answer and perform a modulus 26, so if we had 'z', we substract 'a' thus giving 25 + 1 = 26, modulus 26 = 0. We then add 'a' again and voilà!
** EDIT **
You can even push the concept a little further and add a variable "shifting" value :
Will output
The value of
shiftValue
may be any positive integer (i.e. shifting -2 is the same as shifting 24). Try it.** UPDATE **
Well, just replace your alpha+1 with the equation. Not that I want to feed you everything, however if you must insist, here is what you need to do :
** DISCLAIMER ** : contains your homework solution
Will output
** EDIT 2 **
If you have an index-based alphabet (in case you need to include extra chars, etc.) here's another solution. There is no comment and no optimization, but the code works and should be self explanatory... FYI only :
Naturally, this code will replace every char read from stdin in the
text
string. An example of output would be :;
yet another option, hiding the
==
test in a switch:May be it will useful for someone:
I have the following answer. It takes care of the white spaces in between strings of characters. also when you have a 'z', which is the end of the alphabet, you can't go to the next character, so i made it go to the beginning, which is a.