Q1. Write a program to accept a sentence and count the number of vowels, consonants, digits, special characters, uppercase letters and lowercase letters present in it.
import java.util.Scanner;
class P1{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Sentence: ");
String s = in.nextLine();
int v = 0;
int c = 0;
int d = 0;
int sp = 0;
int u = 0;
int l = 0;
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(Character.isLetter(ch)){
if("AEIOUaeiou".indexOf(ch) >= 0)
v++;
else
c++;
if(Character.isUpperCase(ch))
u++;
else
l++;
}
else if(Character.isDigit(ch))
d++;
else
sp++;
}
System.out.println("Vowels: " + v);
System.out.println("Consonants: " + c);
System.out.println("Uppercase alphabets: " + u);
System.out.println("Lowercase alphabets: " + l);
System.out.println("Digits: " + d);
System.out.println("Special characters: " + sp);
}
}
Q2. Write a program to accept a sentence, convert it into uppercase and count the number of occurrences of each vowel in the converted string.
import java.util.Scanner;
class P2{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Sentence: ");
String s = in.nextLine().toUpperCase();
int a = 0;
int e = 0;
int i = 0;
int o = 0;
int u = 0;
for(int j = 0; j < s.length(); j++){
char ch = s.charAt(j);
switch(ch){
case 'A':
a++;
break;
case 'E':
e++;
break;
case 'I':
i++;
break;
case 'O':
o++;
break;
case 'U':
u++;
break;
}
}
System.out.println("A: " + a);
System.out.println("E: " + e);
System.out.println("I: " + i);
System.out.println("O: " + o);
System.out.println("U: " + u);
}
}
Q3. Write a program to accept a sentence and display the longest word along with its length.
import java.util.Scanner;
class P3{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Sentence: ");
String s = in.nextLine();
String longest = "";
String word = "";
for(int j = 0; j < s.length(); j++){
char ch = s.charAt(j);
if(Character.isLetterOrDigit(ch))
word += ch;
else{
if(longest.length() < word.length())
longest = word;
word = "";
}
}
System.out.println("Longest word: " + longest);
System.out.println("Length: " + longest.length());
}
}
Q4. Write a program to accept two strings and compare them using compareTo() and equalsIgnoreCase(). Display whether the strings are equal and which string comes first alphabetically.
import java.util.Scanner;
class P4{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("First string: ");
String a = in.nextLine();
System.out.print("Second string: ");
String b = in.nextLine();
if(a.equalsIgnoreCase(b))
System.out.println("They are equal.");
else if(a.compareTo(b) < 0)
System.out.println(a + " comes first alphabetically.");
else
System.out.println(b + " comes first alphabetically.");
}
}
Q5. Write a program to accept a sentence, replace every occurrence of the letter ‘E’ with ‘#’ and display the modified sentence.
import java.util.Scanner;
class P5{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Sentence: ");
String s = in.nextLine();
String t = s.replace('E', '#');
System.out.println(t);
}
}
Q6. Write a program to accept a word and display its symbolic word. The symbolic word is formed by extracting all characters from the first vowel onwards, followed by the characters preceding the first vowel and ending with “AY”.
import java.util.Scanner;
class P6{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Word: ");
String w = in.next();
int p = 0;
for(int i = 0; i < w.length(); i++){
char ch = w.charAt(i);
if("AEIOUaeiou".indexOf(ch) >= 0){
p = i;
break;
}
}
String s = w.substring(p) + w.substring(0, p) + "AY";
System.out.println(s);
}
}
Q7. Write a program to accept a sentence and arrange all its words in alphabetical order.
import java.util.Scanner;
class P7{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Sentence: ");
String s = in.nextLine().trim();
int count = 0;
if(s.length() > 0)
count = 1;
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(ch == ' ')
count++;
}
String a[] = new String[count];
String word = "";
int index = 0;
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(Character.isLetterOrDigit(ch))
word += ch;
else{
a[index++] = word;
word = "";
}
}
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a.length - 1 - i; j++){
if(a[j].compareToIgnoreCase(a[j + 1]) > 0){
String temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.println("Words in alphabetical order:");
for(int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}
Q8. Write a program to accept a sentence and a character. Display the number of times the given character occurs in the sentence, ignoring the difference between uppercase and lowercase.
import java.util.Scanner;
class P8{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Sentence: ");
String s = in.nextLine().toUpperCase();
System.out.print("Character: ");
char ch = in.nextLine().charAt(0);
ch = Character.toUpperCase(ch);
int count = 0;
for(int i = 0; i < s.length(); i++){
char chr = s.charAt(i);
if(ch == chr)
count++;
}
System.out.println("Frequency of " + ch + ": " + count);
}
}
Q9. Write a program to accept a sentence and create a new string containing the first and last character of every word in the sentence.
import java.util.Scanner;
class P9{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Sentence: ");
String s = in.nextLine();
String t = "";
String word = "";
for(int j = 0; j < s.length(); j++){
char ch = s.charAt(j);
if(Character.isLetterOrDigit(ch))
word += ch;
else{
t += word.charAt(0);
t += word.charAt(word.length() - 1);
t += " ";
word = "";
}
}
System.out.println("New string: " + t);
}
}
Q10. Bye Bye Transport Company charges for the parcels from Kolkata to Delhi and vice-versa as per the given charges:
| Weight | Charges |
| Up to 10 kg | ₹ 30/kg |
| Next 20 kg | ₹20/kg |
| Next 20 kg | ₹10/kg |
| Above 50 kg | ₹5/kg |
Write a program to input person’s name and parcel weight and calculate the charges to be paid. Display the output in the following format for each customer. Assume that there are 15 parcels.
SL.No. Name Weight Net Amount
import java.util.Scanner;
class P1{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String rows[] = new String[15];
for(int i = 0; i < 15; i++){
System.out.print("Customer name: ");
String name = in.nextLine();
System.out.print("Parcel weight: ");
int weight = Integer.parseInt(in.nextLine());
int amt = 0;
if(weight <= 10)
amt = 30 * weight;
else if(weight <= 30)
amt = 300 + (weight - 10) * 20;
else if(weight <= 50)
amt = 700 + (weight - 30) * 10;
else
amt = 900 + (weight - 50) * 5;
rows[i] = (i + 1) + "\t" + name + "\t" + weight + "\t" + amt;
}
System.out.println("SL.No.\tName\tWeight\tNet Amount");
for(int i = 0; i < rows.length; i++)
System.out.println(rows[i]);
}
}