A sentence in the Special Fashion can be printed by taking two integers (not beyond total number of words in the sentence or less than 1). These integers tell the word number of the sentence. Replace only those words present at those given integer places by the next character in a circular fashion according to the English Alphabets. If both the integers are same then replace only one word. let us consider the following examples:
Input Sentence: He has good Books.
Input Integers: 2, 4
Output Sentence: He ibt good Cpplt.
(i.e. word number 2 and 4 have been replaced by the next characters in a circular fashion)
Input Sentence: Time and tide waits for none.
Input Integers: 3, 3
Output Sentence: Time and ujef waits for none.
Write a case sensitive program that reads a sentence from console (the characters of the sentence may be capital or small or mixed) and two positive integers and output the same sentence after replacing those words present at those given integer places by the next character in a circular fashion according to the English Alphabets.
In the first example given above, word number 2, i.e. “has” is replaced by next characters and hence it becomes “ibt”. Similarly, word number 4, i.e. “Books” is replaced by next characters and hence it becomes “Cpplt”.
import java.io.*;
class Special_Fashion
{
String repChar(String s) //function doing the work of replacing characters
{
int l = s.length(), a = 0;
char ch;
String res="";
for(int i=0; i<l; i++)
{
ch = s.charAt(i); //extracting characters one by one
a = ch + 1; //storing ASCII values after adding 1 to the current character
if(ch=='z' || ch=='Z')
{
a = a - 26;
}
res = res + (char)a; //finally adding the changed character to the new String
}
return res;
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
Special_Fashion ob = new Special_Fashion();
System.out.print("nEnter any sentence : "); //Inputting the sentence
String s = br.readLine();
String ans=""; //String variable to store the final result
String word[]=s.split("[. ]+"); //saving the words of the sentence in an array using split()
int c = word.length;
System.out.print("Enter the 1st word number : ");
int x = Integer.parseInt(br.readLine());
System.out.print("Enter the 2nd word number : ");
int y = Integer.parseInt(br.readLine());
if(x<1 || y<1 || x>c || y>c) //checking whether integers inputted are acceptable or not
{
System.out.println("Sorry! The word numbers inputted are out of range");
}
else
{
if(x != y)
{
word[y-1]=ob.repChar(word[y-1]); //sending the words to the repChar() function
}
word[x-1]=ob.repChar(word[x-1]);
for(int i=0; i<c; i++)
{
ans = ans + word[i] + " ";
}
System.out.print("Output = "+ans.trim()+".");
}
}
}
Input Sentence: He has good Books.
Input Integers: 2, 4
Output Sentence: He ibt good Cpplt.
(i.e. word number 2 and 4 have been replaced by the next characters in a circular fashion)
Input Sentence: Time and tide waits for none.
Input Integers: 3, 3
Output Sentence: Time and ujef waits for none.
Write a case sensitive program that reads a sentence from console (the characters of the sentence may be capital or small or mixed) and two positive integers and output the same sentence after replacing those words present at those given integer places by the next character in a circular fashion according to the English Alphabets.
In the first example given above, word number 2, i.e. “has” is replaced by next characters and hence it becomes “ibt”. Similarly, word number 4, i.e. “Books” is replaced by next characters and hence it becomes “Cpplt”.
import java.io.*;
class Special_Fashion
{
String repChar(String s) //function doing the work of replacing characters
{
int l = s.length(), a = 0;
char ch;
String res="";
for(int i=0; i<l; i++)
{
ch = s.charAt(i); //extracting characters one by one
a = ch + 1; //storing ASCII values after adding 1 to the current character
if(ch=='z' || ch=='Z')
{
a = a - 26;
}
res = res + (char)a; //finally adding the changed character to the new String
}
return res;
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
Special_Fashion ob = new Special_Fashion();
System.out.print("nEnter any sentence : "); //Inputting the sentence
String s = br.readLine();
String ans=""; //String variable to store the final result
String word[]=s.split("[. ]+"); //saving the words of the sentence in an array using split()
int c = word.length;
System.out.print("Enter the 1st word number : ");
int x = Integer.parseInt(br.readLine());
System.out.print("Enter the 2nd word number : ");
int y = Integer.parseInt(br.readLine());
if(x<1 || y<1 || x>c || y>c) //checking whether integers inputted are acceptable or not
{
System.out.println("Sorry! The word numbers inputted are out of range");
}
else
{
if(x != y)
{
word[y-1]=ob.repChar(word[y-1]); //sending the words to the repChar() function
}
word[x-1]=ob.repChar(word[x-1]);
for(int i=0; i<c; i++)
{
ans = ans + word[i] + " ";
}
System.out.print("Output = "+ans.trim()+".");
}
}
}