- Write a program to extract words of a sentence and calculate the potential of the words, print the words with their potential(Potential is the sum of the ASCII values of each letter of the word)
import java.io.*;
class WordPotential
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence");
String str= br.readLine();
str=str+" ";
str=str.toUpperCase();
int len=str.length();
int d=0; int sum=0;
System.out.println("Word "+ "\t" + "Potential" );
for (int i=0;i<len;i++)
{
char ch=str.charAt(i);
if(Character.isWhitespace(ch))
{
String temp=str.substring(d,i);
Temp=temp.trim();
int len2=temp.length();
for(int j=0;j<len2;j++)
{
char ch1=temp.charAt(j);
sum+=(int)ch1;
}
System.out.println(temp+"\t "+sum);;sum=0;d=i;
}
}
}
Output:
Enter a sentence
This is India
Word Potential
THIS 312
IS 156
INDIA 357
2. Write a program to input a word and encrypt it. Each consonant of the word should be replaced with its next letter in the alphabet , and all the vowels will be converted to capital letters
import java.io.*;
class Extract
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a word");
String wrd= br.readLine();
int len=wrd.length();
for (int i=0;i<len;i++)
{
char ch=wrd.charAt(i);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'I':
case 'E':
case 'O':
case 'U':
wrd=wrd.replace(ch,Character.toUpperCase(ch));
break;
default:
wrd=wrd.replace(ch,++ch);
break;
}
}
System.out.println(wrd);
}
}
Output:
Enter a word
Program
QtOhtAn