views:

190

answers:

4

I've reading this question, and I was wondering if Is there any way to consider the whole range of characters? For example, "á", "é", "ö", "ñ", and not consider " " (the [Space])? (For example, my String is "Hello World", and the standard result is "Khoor#Zruog"; I want to erase that "#", so the result would be "KhoorZruog")

I'm sure my answer is in this piece of code:

if (c >= 32 && c <= 127)
        {
            // Change base to make life easier, and use an
            // int explicitly to avoid worrying... cast later
            int x = c - 32;
            x = (x + shift) % 96;
            chars[i] = (char) (x + 32);
        }

But I've tried some things, and it didn't work.

A: 

The Space as the ASCII Code 32 which you don't filter out. You could try:

if (c >= 33 && c <= 127) 
    { 
        // Change base to make life easier, and use an 
        // int explicitly to avoid worrying... cast later 
        int x = c - 32; 
        x = (x + shift) % 96; 
        chars[i] = (char) (x + 32); 
    } 

I just changed the 32 with a 33 in your if-Clause so that the spaces are simply ignored.

A: 

You could use this. It will check for you it the given int value represents a literal. Character

So your function could look like this:

if (Character.isLiteral(c) )
{
     // Change base to make life easier, and use an
     // int explicitly to avoid worrying... cast later
     int x = c - Character.MIN_VALUE;
     x = (x + shift) % Character.MAX_VALUE;
     chars[i] = (char) (x + Character.MIN_VALUE);
}
InsertNickHere
+1  A: 

See this pseudocode - should be trivially implementable:

// you need to define your own range, obviously - it's not at all obvious whether
// e.g. "ź" should be included and that it should come after "z"
array char_range = ['a','á','b','c','č', (...), 'z','ź','ž'] 
// the text to encode
string plaintext = 'some text here'
// this will contain encoded text
stringbuilder ciphertext = ''
// the classic Caesar Cipher shifts by 3 chars to the right
// to decipher, reverse the sign
int shift_by = 3 
// note: character != byte, esp. not in UTF-8 (1 char could be 1 or more bytes)
for each character in plaintext
    get character_position of character in char_range // e.g. "a" would return 0
    if not in char_range // e.g. spaces and other non-letters
        do nothing // drop character 
        // alternately, you can append it to ciphertext unmodified
        continue with next character
    add shift_by to character_position
    if character_position > char_range.length
        character_position modulo char_range.length
    if character_position < 0 // useful for decoding
        add char_range.length to character_position 
    get new_character at character_position
    append new_character to ciphertext
done
Piskvor
Of course, while this is a nice excercise, it is not really cryptography - i.e. is breakable by anyone over the age of 5, within 5 minutes.
Piskvor
A: 

Hello, again! Thanks por your answers. So, my code is something like this:

import java.lang.Character;
import java.io.*;

public class Pto10{
public static void main (String args[]){
    String text="Hello World";
    int aumento=3;
    char[] aCaracteres = text.toCharArray();
    for (int i=0; i < aCaracteres.length; i++){
        char c = aCaracteres[i];
        if (Character.isLiteral(c) ){
            int x = c - Character.MIN_VALUE;
            x = (x + aumento) % Character.MAX_VALUE;
            aCaracteres[i] = (char) (x + Character.MIN_VALUE);
                                    }
                                            }
    System.out.println(aCaracteres);
}
}

But when I try to compile it, it says: "cannot find symbol method isLiteral(char) line 11", which is this line:

 if (Character.isLiteral(c) ){

What am I doing wrong? Thanks for everything!

Rodolfo