Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

lower case & upper case  RSS feed

 
Felix Mauser
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a method that encrypts a String using a Key that can have upper and lower case letters in it. Only a-z, A-Z no other characters -- spaces and any other characters are simply remain the same. I have to maintain the case of the original String. So for example, if I have an input string of "Fred and Barney are friends" and the key is "QwertYuiopasdfgHJklzxcvbnm" the output would be "Yktr qfr Wqkftn qkt ykotfrl" -- Thanks for your input!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

OK, so, what do you have so far?
 
Felix Mauser
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static String keyEncrypt(String inString, String key)
throws IOException, InvalidKeyLengthException, InvalidKeyException{
keyCheck(key);

char[] output = inString.toCharArray();
String outputString = "";
{not sure what do here because I don't know how to deal with
upper and lower case"

I would basically have another char array with the key
do some kind of comparison using a nested loop
and with each match..
update output[i] to corresponding key character,
then outputString = outputString + output[i]

return outputString
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Felix Mauser:
...not sure what do here because I don't know how to deal with
upper and lower case...

Here are two approaches for working with the chars...
  • ASCII table (for char values)
  • java.lang.Character
  •  
    Felix Mauser
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the links. I understand that characters have a numerical value, but I still don't see how to accomplish what I am trying to do. I don't understand how I can compare the "B" from my input String to the key which has a lower case "w" (for the letter b) then have my output be an uppercase "W"

    Thanks again!
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12542
    48
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    does the case of the key matter? in your example, you give a key of "QwertYuiopasdfgHJklzxcvbnm". Why are some of these up-cased and some not?

    if it doesn't make any difference, the first thing i'd do is normalize the string so that my key was all upper or lower.

    do you need to validate the key to make sure each letter is in there once and only once?

    something to consider... notice that to change an 'a' to a 'q' requires the same change in value as changing an 'A' to a 'Q'.
     
    Felix Mauser
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yes the key can contain Upper and lowercase, so for sure I would take the key string and create key[] = key.toLowerCase().toCharArray()

    then I could compare each letter of the inputString (string to be coded) and if it's between 'a' and 'z' then the output would be key[inputChar-'a'] (I think)

    but if the letter of the inputString is between 'A' and 'Z' how do I compare it to the key and output an uppercase character

    I know I am close, but I am just to dense to see it!! AACK!
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Felix Mauser:
    ...I understand that characters have a numerical value, but I still don't see how to accomplish what I am trying to do. I don't understand how I can compare the "B" from my input String to the key which has a lower case "w" (for the letter b) then have my output be an uppercase "W"...

    Well, you know 'B' is uppercase (either because of the range its value falls in, or by using a method in Character to test it). So when it translates to 'w' you know you need to convert that 'w' to uppercase. Look again at the ASCII table, and see if you can find a relationship between a lowercase character and its uppercase version.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12542
    48
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok, here is a hint at another approach.

    you sound like you know what to do if it's all lower case.

    you could change it slightly to not alter any letters in uppercase, but just pass them through untouched. so for a key of "def", you could change the inputstring of "aBc" to "dBf".

    now, what if you make a second pass through the string, but the other way around?
     
    Felix Mauser
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the help guys. I finally came up with something. It sure ain't pretty, but it works. Thankfully efficiency and style don't count right now. It just has spit out the right stuff.

    public static String keyEncrypt(String inString, String key)
    throws IOException, InvalidKeyLengthException, InvalidKeyException{
    keyCheck(key);
    key = key.toLowerCase();
    char[] keylet = key.toCharArray();
    char[] output = inString.toCharArray();
    String outputString = "";

    for(int i=0; i<output.length; i++){
    if('a' <= output[i]&& output[i] <= 'z'){
    output[i] = keylet[((int)output[i])-'a'];
    }
    else if('A' <= output[i]&& output[i] <= 'Z'){
    output [i] = (char) (keylet[((int) output[i])-'A']+('A'-'a'));
    }

    outputString = outputString + output[i];
    }
    return outputString;
    }

    Thanks again
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Felix Mauser:
    ...It sure ain't pretty, but it works...

    To start making this a bit more readable, you might define some constants, like...

    static final int LETTER_OFFSET = 65; // value of 'A'
    static final int LETTER_CASE_OFFSET = 32; // 'a' - 'A'

    Then...

    (char) (keylet[((int) output[i])-'A']+('A'-'a'));

    ...could be rewritten more clearly as...

    (char)(keylet[output[i] - LETTER_OFFSET] - LETTER_CASE_OFFSET);
    [ December 13, 2006: Message edited by: marc weber ]
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!