Each digit will be substituted by adding 7 and taking the remainder and
dividing it by 10
The program needs to perform and displays the result of encrypting the
text as follows: Each letter is substituted by the letter that is in the
9th position after its own position in the ASCII sequence for the alphabet
– whether capital or small letter. Assume a continuous "loop" of the
alphabet letters for a-z and A-Z [for instance: the letter 'Y' will be
substituted by the letter 'H'], and each digit will be substituted by
adding 7 and taking the remainder of dividing it by 10 [for instance: the
digit for 4 will be substituted by 1].
package Java;
public class SimpleWordProcessor {
static String sentence = "Welcome to the Spring 2013 Semester. We are
learning Object-Oriented Programming in CSCI 1302.";
static String vowels = "aeiou";
static String consonants = "bcdfghjklmnpqrstvwxyz";
static String punctuation = ".,:;?!";
static String digits = "0123456789";
static String encryptedSentence = new String(sentence);
static String reverseEncryption = new String(sentence);
static void encryptSentence() {
encryptedSentence = "";
for (int i = 0; i < sentence.length(); i++) {
char c = sentence.charAt(i);
boolean d = sentence.equals(i);
char replace = c;
if (c >= 'a' && c <= 'z') {
replace = (char) (c + 9);
if (replace > 'z') {
replace = (char) (replace - 26);
}
}
if (c >= 'A' && c <= 'Z') {
replace = (char) (c + 9);
if (replace > 'Z') {
replace = (char) (replace - 26);
}
}
encryptedSentence += replace;
}
}
static void reverseEncryption() {
reverseEncryption = "";
for (int i = 0; i < sentence.length(); i++) {
char c = sentence.charAt(i);
char replace = c;
if (c >= 'a' && c <= 'z') {
replace = (char) (c - 9);
if (replace > 'z') {
replace = (char) (replace - 26);
}
}
if (c >= 'A' && c <= 'Z') {
replace = (char) (c - 9);
if (replace > 'Z') {
replace = (char) (replace - 26);
}
}
reverseEncryption += replace;
}
}
public static void main(String[] args) {
int countVowels = 0, countConsonants = 0, countSpaces = 0,
countPunctuation = 0, countDigits = 0, countWordcount = 1;
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ' ' && sentence.charAt(i + 1) != ' ') {
countWordcount++;
}
}
for (char c : sentence.toLowerCase().toCharArray()) {
if (c == ' ') {
countSpaces++;
} //else if(wordcount.indexOf(c)>=0) countWordcount++;
else if (vowels.indexOf(c) >= 0) {
countVowels++;
} else if (consonants.indexOf(c) >= 0) {
countConsonants++;
} else if (punctuation.indexOf(c) >= 0) {
countPunctuation++;
} else if (digits.indexOf(c) >= 0) {
countDigits++;
}
}
System.out.println("# of Vowels: " + countVowels);
System.out.println("# of Consonants: " + countConsonants);
System.out.println("# of Punctuation: " + countPunctuation);
System.out.println("# of Spaces: " + countSpaces);
System.out.println("# of Digits: " + countDigits);
System.out.println("Word Count: " + countWordcount);
System.out.println("Original sentence: " + sentence);
encryptSentence();
System.out.println("Encrypted sentence: " + encryptedSentence);
reverseEncryption();
System.out.println("Reverse Encryption: " + reverseEncryption);
}
}
No comments:
Post a Comment