牛客網——華爲機試(題29:字符串加解密)(Java)

題目描述:

1、對輸入的字符串進行加解密,並輸出。

2加密方法爲:

當內容是英文字母時則用該英文字母的後一個字母替換,同時字母變換大小寫,如字母a時則替換爲B;字母Z時則替換爲a;

當內容是數字時則把該數字加1,如0替換1,1替換2,9替換0;

其他字符不做變化。

3、解密方法爲加密的逆過程。

 

接口描述:

    實現接口,每個接口實現1個基本操作:

void Encrypt (char aucPassword[], char aucResult[]):在該函數中實現字符串加密並輸出

說明:

1、字符串以\0結尾。

2、字符串最長100個字符。

 

int unEncrypt (char result[], char password[]):在該函數中實現字符串解密並輸出

說明:

1、字符串以\0結尾。

    2、字符串最長100個字符。

輸入描述:

輸入說明
輸入一串要加密的密碼
輸入一串加過密的密碼

輸出描述:

輸出說明
輸出加密後的字符
輸出解密後的字符

示例1:

輸入:

abcdefg
BCDEFGH

輸出:

BCDEFGH
abcdefg

代碼: 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
	public static void main ( String[] args ) throws IOException {
		BufferedReader bf = new BufferedReader( new InputStreamReader( System.in ) );
		String s ;
		while( ( s = bf.readLine() ) != null ) {
			
			char aucPassword[] = s.toCharArray();
			char result[] = bf.readLine().toCharArray();
			
			int length1;
			if ( aucPassword.length <= 100 ) {
				length1 = aucPassword.length;
			}
			else {
				length1 = 100;
			}
			char aucResult[] = new char[ length1 ];
			
			int length2;
			if ( result.length <= 100 ) {
				length2 = result.length;
			}
			else {
				length2 = 100;
			}
			char password[] = new char[ length2 ];
			
			Encrypt ( aucPassword , aucResult );
			unEncrypt ( result , password );
		}
		bf.close();
	}
	
	static void Encrypt (char aucPassword[], char aucResult[]) {
		
		for ( int i = 0 ; i < aucResult.length ; i++ ) {
			if ( aucPassword[ i ] >= 65 && aucPassword[ i ] <= 90 ) {
				if ( aucPassword[ i ] == 'Z' ) {
					aucResult[ i ] = 'a';
				}
				else {
					aucResult[ i ] = (char) (aucPassword[ i ] + 33 );
				}
			}
			
			else  if ( aucPassword[ i ] >= 97 && aucPassword[ i ] <= 122 ) {
				if ( aucPassword[ i ] == 'z' ) {
					aucResult[ i ] = 'A';
				}
				else {
					aucResult[ i ] = (char) (aucPassword[ i ] - 31 );
				}
			}
				
			else if ( aucPassword[ i ] >= 48 && aucPassword[ i ] <= 57 ) {
				if ( aucPassword[ i ] == '9' ) {
					aucResult[ i ] = '0';
				}
				else {
					aucResult[ i ] = (char) (aucPassword[ i ] + 1 );
				}
			}
				
			else {
				aucResult[ i ] = aucPassword[ i ];
			}
		}
			
		System.out.println( String.valueOf( aucResult ) );
	}
	
	static int unEncrypt (char result[], char password[]) {
		for ( int i = 0 ; i < password.length ; i++ ) {
			if ( result[ i ] >= 65 && result[ i ] <= 90 ) {
				if ( result[ i ] == 'A' ) {
					password[ i ] = 'z';
				}
				else {
					password[ i ] = (char) ( result[ i ] + 31 );
				}
			}
			else if ( result[ i ] >= 97 && result[ i ] <= 122 ) {
				if ( result[ i ] == 'a' ) {
					password[ i ] = 'Z';
				}
				else {
					password[ i ] = ( char )( result[ i ] - 33 );
				}
			}
			else if ( result[ i ] >= 48 && result[ i ] <= 57 ) {
				if ( result[ i ] == '0' ) {
					password[ i ] = '9';
				}
				else {
					password[ i ] = ( char )( result[ i ] - 1 );
				}
			}
			else {
				password[ i ] = result[ i ];
			}
		}
		System.out.println( String.valueOf( password ) );
		return 0;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章