How to shift a character in a string to the character three before it in ASCII table?
-
Hi it's me requiring help with my CS assignment. So, I have to make a program where the user inputs a 7-word sentence with integers and encrypt it. I have to make a method for each of the words. I am all done with 1,3,4,5,6,7. But I don't know how to encrypt number 2. On the requirements sheet, it says: > Word #2: exchange first and last character, the rest of the characters are shifted to the character three before it in the ASCII table (works for non-letters as well.) Sunrise-->erkofpS I got the exchanging of the first and last character done (pretty much) : > public static String word2 (String input){ > String noval = " "; //Used for spacing > char firstletter = input.charAt(input.length()-1); > char lastletter = input.charAt (0); > > System.out.print (firstletter); > for (int i = 2; i < (input.length()-1); i++){ > System.out.print ((input)); > break; > } > System.out.print (lastletter); > return noval;//Return nothing, just used for spacing > } If the program was run, and the user enters "**Sunshine**" as (String input) in the main method, the word2 method will output this : eSunshineS I know that this isn't right. So, what should I do to so that the characters are shifted to the character three before it in the ASCII table. (not the exchanged ones). It would be better if it only includes changing parts of the 'for' loop. Thanks in advance.
-
Answer:
String noval = " "; //Used for spacing String input = "Sunrise"; char firstletter = input.charAt(input.length() - 1); char lastletter = input.charAt(0); System.out.print(firstletter); for (int i = 1; i < (input.length() - 1); i++) { char charAti = input.charAt(i); int asciiCharAti = (int)charAti; asciiCharAti = asciiCharAti - 3; char newCharAti = (char)asciiCharAti; System.out.print(newCharAti); } System.out.println(lastletter);
Shifat Taushif at Yahoo! Answers Visit the source
Other answers
Simple subtraction works for characters. char - 3 is all you need. I know it isn't very intuitive because of the type difference, but such is life :)
coachabower
Related Q & A:
- How to make parentheses bold in a string?Best solution by Stack Overflow
- How safe is a career as a CNA in a nursing home?Best solution by Yahoo! Answers
- How to transfer a video from a camera to a PC?Best solution by Super User
- How much will a upgrade to a iPhone 4 on a family plan cost?Best solution by ChaCha
- How to connect a Bluetooth to a Samsung Sleek without a manual?Best solution by fixyourthinking.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.