I'm trying to write a code changes the characters in a string that I enter. This is the mapping that I have to follow when changing the characters.
String letters = "abcdefghijklmnopqrstuvwxyz";
String enc = "kngcadsxbvfhjtiumylzqropwe";
By searching through stackoverflow I found out that a string cannot be changed, so I need to create a new string with the converted characters. My problem is that I don't know how to do that. So far I have been able to access each character in the string and print them. I know the solution to this is to access each character, change them then add them to the new string, this is what I don't know how to do or to look up.
public static void main(String[] args)
{
int cdcode;
//String letters = "abcdefghijklmnopqrstuvwxyz";
//String enc = "kngcadsxbvfhjtiumylzqropwe";
String myString;
char ch;
int strLen;
int chrPos;
do
{
System.out.println("Enter 1 to encode, 2 to decode, 3 to quit:");
Scanner myCode = new Scanner(System.in);
Scanner myText = new Scanner(System.in);
cdcode = myCode.nextInt();
switch(cdcode)
{
case 1:
System.out.println("Enter the text to encode: ");
myString = myText.nextLine();
strLen = myString.length();
System.out.println("String Length is: " +myString.length());
for(chrPos = 0; chrPos < strLen ;chrPos++)
{
System.out.println("The Letters are: " +myString.charAt(chrPos));
if(myString.charAt(chrPos)== 'S')
{
System.out.println("It is an S");
}
}
break;
case 2:
System.out.println("Enter the text to decode: ");
myString = myText.nextLine();
break;
}
}while(cdcode != 3);
}