Given a string the task is to change all the letter of that string to the preceding letter. The letter ‘A’ or ‘a’ must be changed to ‘Z’ or ‘z’ respectively.
Example: Consider a word “Apple”. The program outputs “Zookd”.
Java
import java.util.*;
public class PreviousLetter
{
public static void main(String args[])
{
int l=0;
char ch=' ';
String str="",result="";
Scanner sc=new Scanner(System.in);
System.out.print("Enter a word: ");
str=sc.next();
str=str.trim();
l=str.length();
for(int i=0;i<l;i++)
{
ch=str.charAt(i);
if(ch=='a')
{
result=result+'z';
}
else if(ch=='A')
{
result=result+'Z';
}
else
{
result=result+(char)(ch-1);
}
}
System.out.println("New word: "+result);
}
}Java