Practice Sheet
Topic: String
Task 1: Write a code that takes a user input and checks whether that word is a palindrome or
not.
[A word is a palindrome when you read it forwards or backwards it still spells the same for
example: radar, mom, madam etc]
Input Output
radar radar is a palindrome!
antonym antonym is NOT a palindrome!
Task 2: Write a code that will take input from the user and count the number of vowels and
consonants in that word.
Input Output
abracadabra Vowels: 5
Consonant: 6
glyph Vowels: 0
Consonant: 5
Abacus1% Vowels: 3
Consonant: 3
Task 3: Write a code that will take input from the user and find the unique letters in that word.
[Note: You can assume user will always input lowercase letters]
Input Output
erroneous Unique letters: eronus
engineering Unique letters: engir
andn23%4 Unique letters: and
Solution
- Must try to solve it by yourself first!
- Only look at the solution when you get stuck
- Each problem can be solved in different ways
- As long as you get the correct answer following the conditions mentioned in the
question
Task 1:
import java.util.Scanner;
public class WorkTask1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
boolean flag = true;
for(int i = 0; i < s.length() / 2; i++){
if(s.charAt(i) != s.charAt(s.length() - i - 1)){
flag = false;
break;
}
}
if(flag){
System.out.println(s + " is a palindrome!");
}
else{
System.out.println(s + " is NOT a palindrome!");
}
}
}
Task 2:
import java.util.Scanner;
public class WorkTask2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int vowelCount = 0, consonantCount = 0;
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){
vowelCount++;
}
else if(c >= 'a' && c <= 'z'){
consonantCount++;
}
}
System.out.println("Vowels: " + vowelCount);
System.out.println("Consonant: " + consonantCount);
}
}
Task 3:
import java.util.Scanner;
public class WorkTask3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String store = "";
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(c >= 'a' && c <= 'z'){
boolean flag = false;
for(int j = 0; j < store.length(); j++){
if(c == store.charAt(j)){
//if the char is already in store
//then no need to add it again
flag = true;
break;
}
}
//only when the loop is not broken
//means a new char has appeared
//so that "new char" has to be added
if(flag == false) store += c;
}
}
System.out.println("Unique letters: " + store);
}
}
Topic: Linear Array
Task 4: For a given array of Strings, write the code that will print the number of words that are
palindrome.
Given Array Output
String [] arr = {"mom", "1aa1", "tree", "kayak"}; Palindrome count = 3
Task 5: For a given array of Strings, write the code that will print the word whose sum of ASCII
characters is the largest.
Given Array Output
String [] arr = {"Feluda", "Batman", "Sherlock", Word: Sherlock
"Conan"}; index: 2
sum: 827
Solution:
Try for yourself!
Topic: 2D Array
Task 6: For a given 2D Array, write the code that will print the number of odd numbers in each
row.
Given Array Output
int [][] arr = {{4, 5, 6, 9}, {1, 2, 4}, {11, 12, 13}}; Row 1 odd count = 2
Row 2 odd count = 1
Row 3 odd count = 2
Task 7: For a given 2D Array, write the code that will print the number of even numbers in each
column. [Assume a square matrix will be provided]
Given Array Output
int [][] arr = {{4, 5, 6}, {1, 2, 4}, {11, 12, 13}}; Column 1 even count = 1
Column 2 even count = 2
Column 3 even count = 2
Solution:
Try for yourself!