-3

I want to encypt a string by adding +1 (ascii) to every char in the string This my attempt

public static string encrypt(string str){
for(int i = 0; i < str.length(); i++){
    int x = str.charAt(i) ;
    x = x + 1; 
}

// Now how can I complete this loop to produce a new string with encrypting the string by adding 1 to every char?

Michael
  • 41,989
  • 11
  • 82
  • 128
Yousef HM
  • 1
  • 2
  • Note that +1 on an integer type like char produces round robin from 32767 to -32768 or from 65535 to 0. Also, I believe java follows the straightforward 16-bit encoding of its character set (the BMP of the UCS) meaning "+1" may not necessarily yield the code point number of a genuine valid character in the character set. See e.g. the grey boxes at https://en.wikibooks.org/wiki/Unicode/Character_reference/2000-2FFF . All that kind of stuff ***may*** produce "funny" results. – Erwin Smout Feb 04 '19 at 14:18

1 Answers1

0

I suggest you try the following :

Public static string encrypt(string str){
    String result = "";   
    for(int i=0; i<str.length() ; i++){
        int x = str.charAt(i) ;
        x = x+1 ;
        result+= Character.toString((char)x);
    }
    return result;
 }
Youssef NAIT
  • 1,362
  • 11
  • 27