NOTE: This question is from a SPECIMEN PAPER released by the council
The potential of a word is found by adding the ASCII value of the alphabets. (ASCII values of A to Z are 65 to 90). Example: BALL Potential = 66 + 65 + 76 + 76 = 283 Write a program to accept a sentence which may be terminated by either “ . ” , “ ? ” or “ ! ” only. The words of sentence are separated by single blank space and are in UPPER CASE. Decode the words according to their potential and arrange them in ascending order of their potential strength. Test your program with the following data and some random data:
The potential of a word is found by adding the ASCII value of the alphabets. (ASCII values of A to Z are 65 to 90). Example: BALL Potential = 66 + 65 + 76 + 76 = 283 Write a program to accept a sentence which may be terminated by either “ . ” , “ ? ” or “ ! ” only. The words of sentence are separated by single blank space and are in UPPER CASE. Decode the words according to their potential and arrange them in ascending order of their potential strength. Test your program with the following data and some random data:
Example 1:
INPUT: HOW DO YOU DO?
OUTPUT:
HOW = 238 DO = 147 YOU = 253 DO = 147
DO DO HOW YOU
Example 2:
INPUT: LOOK BEFORE YOU LEAP.
OUTPUT: LOOK = 309 BEFORE = 435 YOU = 253 LEAP = 290
YOU LEAP LOOK BEFORE
Example 3: INPUT: HOW ARE YOU#
OUTPUT: INVALID INPUT
The logic behind the program
1. Input the sentence by checking its validity.
2. Count the number of words in the sentence by counting number of spaces.
3. Extract the words into a String array
4. For each word calculate its potential and store its value in some integer array
5. Sort both the integer array and the integer array in the same loop
6. Print the sorted arrays.
For step 1 the followind code has been used
public void wordCount() {
for(int i=0;i<msg.length();i++)
if(msg.charAt(i)== ' ')
word_counter++;
word_counter +=1;
word_Storage = new String[word_counter];
potential_of_word = new int[word_counter];
}
For step 3 the following code has been used
For step 3 the following code has been used
public void wordExtractor() {
int k = 0;
for(int i=0;i<msg.length();i++) {
word_Storage[k]="";
while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.' && msg.charAt(i) != '!') {
word_Storage[k] +=msg.charAt(i);
i++;
}
k++;
}
For calculatin the potential of the ith word we have used the below code:
for(int j=0;j<word_Storage[i].length();j++)
tmp +=word_Storage[i].charAt(j);
For sorting both the arrays int[] potential_of_word and String[] word_Storage
for(int k=0;k<word_counter;k++)
for(int l=k;l<word_counter;l++) {
if (potential_of_word[k] > potential_of_word[l]) {
tmp = potential_of_word[l]; s= word_Storage[l];
potential_of_word[l] = potential_of_word[k]; word_Storage[l] = word_Storage[k];
potential_of_word[k] = tmp; word_Storage[k] = s;
}
}
complete JAVA CODE
complete JAVA CODE
import java.io.*;
public class ISC2020Q2 {
public static void main(String[] args) throws IOException{
String Sentence;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the sentence:");
Sentence=br.readLine();
Sentence = Sentence.toUpperCase();
while(Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '!' ) {
System.out.println("INVALID INPUT:,TRY AGAIN");
Sentence=br.readLine();
}
Potential obj = new Potential(Sentence);
obj.wordCount();
obj.wordExtractor();
System.out.println("OUTPUT:");
obj.countAndDisplayPotential();
br.close();
}
}
class Potential{
public void countAndDisplayPotential() {
int tmp;
String s;
for(int i=0;i<word_counter;i++) {
tmp = 0;
for(int j=0;j<word_Storage[i].length();j++)
tmp +=word_Storage[i].charAt(j);
potential_of_word[i]=tmp;
System.out.println(word_Storage[i]+"="+potential_of_word[i]);
}
for(int k=0;k<word_counter;k++)
for(int l=k;l<word_counter;l++) {
if (potential_of_word[k] > potential_of_word[l]) {
tmp = potential_of_word[l]; s= word_Storage[l];
potential_of_word[l] = potential_of_word[k]; word_Storage[l] = word_Storage[k];
potential_of_word[k] = tmp; word_Storage[k] = s;
}
}
System.out.println();
for(int m=0;m<word_counter;m++)
System.out.print(word_Storage[m]+" ");
}
public void wordExtractor() {
int k = 0;
for(int i=0;i<msg.length();i++) {
word_Storage[k]="";
while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.' && msg.charAt(i) != '!') {
word_Storage[k] +=msg.charAt(i);
i++;
}
k++;
}
}
public void wordCount() {
for(int i=0;i<msg.length();i++)
if(msg.charAt(i)== ' ')
word_counter++;
word_counter +=1;
word_Storage = new String[word_counter];
potential_of_word = new int[word_counter];
}
Potential (String Sentence) {
msg = Sentence;
word_counter = 0;
}
private int[] potential_of_word;
private int word_counter;
private String msg;
private String[] word_Storage;
}
import java.io.*;
public class ISC2020Q2 {
public static void main(String[] args) throws IOException{
String Sentence;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the sentence:");
Sentence=br.readLine();
Sentence = Sentence.toUpperCase();
while(Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '!' ) {
System.out.println("INVALID INPUT:,TRY AGAIN");
Sentence=br.readLine();
}
Potential obj = new Potential(Sentence);
obj.wordCount();
obj.wordExtractor();
System.out.println("OUTPUT:");
obj.countAndDisplayPotential();
br.close();
}
}
class Potential{
public void countAndDisplayPotential() {
int tmp;
String s;
for(int i=0;i<word_counter;i++) {
tmp = 0;
for(int j=0;j<word_Storage[i].length();j++)
tmp +=word_Storage[i].charAt(j);
potential_of_word[i]=tmp;
System.out.println(word_Storage[i]+"="+potential_of_word[i]);
}
for(int k=0;k<word_counter;k++)
for(int l=k;l<word_counter;l++) {
if (potential_of_word[k] > potential_of_word[l]) {
tmp = potential_of_word[l]; s= word_Storage[l];
potential_of_word[l] = potential_of_word[k]; word_Storage[l] = word_Storage[k];
potential_of_word[k] = tmp; word_Storage[k] = s;
}
}
System.out.println();
for(int m=0;m<word_counter;m++)
System.out.print(word_Storage[m]+" ");
}
public void wordExtractor() {
int k = 0;
for(int i=0;i<msg.length();i++) {
word_Storage[k]="";
while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.' && msg.charAt(i) != '!') {
word_Storage[k] +=msg.charAt(i);
i++;
}
k++;
}
}
public void wordCount() {
for(int i=0;i<msg.length();i++)
if(msg.charAt(i)== ' ')
word_counter++;
word_counter +=1;
word_Storage = new String[word_counter];
potential_of_word = new int[word_counter];
}
Potential (String Sentence) {
msg = Sentence;
word_counter = 0;
}
private int[] potential_of_word;
private int word_counter;
private String msg;
private String[] word_Storage;
}
No comments:
Post a Comment