Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math 'floor' and 'ceil' function question 1

Status
Not open for further replies.

nerd4hire

Programmer
Oct 14, 2001
2
US
I am working on an encrypt / decrypt function. The encrypt part of it removes all spaces between characters (if there is any) then 'packs' the string into a condensed form. It then takes every other character and puts each alternating character in alternating strings. IE string "ABCD" ~> String1 = "AC" String2= "BD". it then adds them together to form String_encrypted = "ACBD".

My question is, I have to find exactly half the length and split the two strings into seperate strings again to decrypt. This works great if the string is even, however when the string is odd it rounds down because i have an int. I have been unsuccessful in using the floor or ceiling funtion in java, If someone could PLEASE just show me what I am doing wrong here is what I have...


public String railFenceDecode( String rail_fence_encoded ){


String s1 = "";
String s2 = "";

String rail_fence_packed_encoded = railFenceEncode(rail_fence_encoded);

double length.ceil((rail_fence_packed_encoded.length() / 2) + .4 );

String rail_fence_decoded = rail_fence_packed_encoded.substring(length);




// String rail_fence_decoded = rail_fence_packed_encoded;
return rail_fence_decoded;

}
 
The following code should do the trick:

-------------------------------------


import java.lang.*;

...

//give length of first half : length/2 if length is even,
//length/2 + 1 if length is odd.
int length = (int)
Math.ceil((double) rail_fence_packed_encoded.length()/2);

 
Thank you so much for your help. Here is my entire program. It consist of several different encryption methods, and one encrypt/decrypt function.

Enjoy

~Roy~


import java.lang.*;

class StringEncrypt_1 {

public static void main( String [] args ){

if( args.length != 1 ){
System.out.println( "usage: java StringEncrypt_1 secret-message" );
return;
}
String original = args[0]; //uses whatever you gave as the command-line arg
StringEncrypt_1 the_object = new StringEncrypt_1();

the_object.leftEncryptTest1( original );
the_object.leftEncryptTest2( original );
the_object.rightEncryptTest( original );
the_object.replaceEncryptTest( original );
the_object.paranoidEncryptTest( original );
the_object.isMarkerMessageTest( original );
the_object.railTest( original );
} //main


public String leftEncrypt( String s ){

return leftEncrypt( s, 1 ); //reduced body to 1 line!

} //leftEncrypt


public String leftEncrypt( String s, int i ){

return swapPieces( s, i);

} //leftEncrypt


public String swapPieces( String s, int i ){

String left_piece = s.substring(0,i); //step 1
//System.out.println( "left_piece=" + left_piece );
String right_piece = s.substring(i) ; //step 2
//System.out.println( "right_piece=" + right_piece );
String reorder = right_piece + left_piece ; //step 3 (glue them together)
//System.out.println( "glued back=" + reorder );
return reorder ; //step 4

} //swapPieces

public String rightEncrypt ( String s, int i ) {

return swapPieces(s, s.length()-i );
}

public String replaceEncrypt( String s ){

/* comment to describe how method works:
a <=> r
e <=> s
i <=> n
o <=> t
u <=> g
*/

String result = s; //just an alias

result = result.replace( 'a', 'R' );
//System.out.println( &quot;step1:&quot; + result );
result = result.replace( 'r', 'A' );
//System.out.println( &quot;step2:&quot; + result );

result = result.replace( 'e', 'S' );
result = result.replace( 's', 'E' );

result = result.replace( 'i', 'N' );
result = result.replace( 'n', 'I' );

result = result.replace( 'o', 'T' );
result = result.replace( 't', 'O' );

result = result.replace( 'u', 'G' );
result = result.replace( 'g', 'U' );



return result.toLowerCase();

}

public String paranoidEncrypt( String s ){

String left_shift_1 = leftEncrypt( s ); //use leftEncrypt,
String replaced = replaceEncrypt( left_shift_1 ); //then pass results to replaceEncrypt
return replaced;

} //paranoidEncrypt

public boolean isMarkerMessage( String s ){

boolean b1 = s.equals(leftEncrypt ( s) ) ;
return b1;

}


public String pack( String s ){
String packed = &quot;&quot;;
final char SPACE = ' ';

for( int i = 0; i < s.length(); i++ )
if( s.charAt( i ) != SPACE ) packed = packed + s.charAt( i );

return packed;
}


public String railFenceEncode( String packed ){

String pack = pack(packed);
String s1 = &quot;&quot;;
String s2 = &quot;&quot;;

for( int j = 0; j < pack.length(); j++ ){

s1 = s1 + pack.charAt(j);
if(j < pack.length() - 1) {
j++;
s2 = s2 + pack.charAt(j);
}
}

String rail_fence_encoded = s1.concat(s2);

return rail_fence_encoded;

}

public String railFenceDecode( String rail_fence_encoded ){

String rail_fence_decoded = &quot;&quot;;

String rail_fence_packed_encoded = railFenceEncode(rail_fence_encoded);

int length = (int)
Math.ceil((double) rail_fence_packed_encoded.length()/2);

String s1_encoded = rail_fence_packed_encoded.substring(0, length);
String s2_encoded = rail_fence_packed_encoded.substring(length);

for( int k = 0; k < s1_encoded.length(); k++ ){

rail_fence_decoded = rail_fence_decoded + s1_encoded.charAt(k);
if(k < s2_encoded.length() ) rail_fence_decoded = rail_fence_decoded + s2_encoded.charAt(k);

}

return rail_fence_decoded;

}


//////////////// Begin Test Methods /////////////////////////

private void leftEncryptTest1( String clear ){
String encrypted = leftEncrypt( clear );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;leftEncrypt1: &quot; + encrypted );
System.out.println( ); //blank line
} //leftEncryptTest1


private void leftEncryptTest2( String clear ){
String encrypted2 = leftEncrypt( clear, 2 );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;leftEncrypt2: &quot; + encrypted2 );
System.out.println( );

String encrypted3 = leftEncrypt( clear, 3 );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;leftEncrypt3: &quot; + encrypted3 );
System.out.println( );
} //leftEncryptTest2

private void rightEncryptTest( String clear ){
String encrypted2 = rightEncrypt( clear, 2 );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;rightEncrypt2:&quot; + encrypted2 );
System.out.println( );

System.out.println( &quot;Clear: &quot; + clear );
String encrypted3 = rightEncrypt( clear, 3 );
System.out.println( &quot;rightEncrypt3:&quot; + encrypted3 );
System.out.println( );
} //rightEncryptTest

private void replaceEncryptTest( String clear ){
String encrypted = replaceEncrypt( clear );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;replEncrypt: &quot; + encrypted );
System.out.println( );
} //replaceEncryptTest


private void paranoidEncryptTest( String clear ){
String encrypted = paranoidEncrypt( clear );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;paranEncrypt: &quot; + encrypted );
System.out.println( );
} //paranoidEncryptTest

private void isMarkerMessageTest( String clear ){
boolean check = isMarkerMessage( clear );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;Marker check: &quot; + check );
System.out.println( );
} //isMarkerMessageTest

private void railTest( String clear ){
String packed = pack( clear );
String rail_fence_encoded = railFenceEncode( clear );
String rail_fence_decoded = railFenceDecode( clear );
System.out.println( &quot;rail fence encoding = &quot; + rail_fence_encoded );
System.out.println( &quot;rail fence decoding = &quot; + rail_fence_decoded );
System.out.println( &quot;clear =&quot; + clear );
} //railTest




} //StringEncrypt_1

 
You're welcome.

And thanks for sharing the result:
I will definitely try it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top