ICSE Java Programming Strings 2022 Q7 SPECIMEN PAPER Solved
Question 7
Define a class to accept and store 10 strings into the array and print the strings with even number of characters.
import java.util.Scanner;
public class ICSEJava {
public static void main(String[] args) {
EvenCharacters obj = new EvenCharacters();
obj.inputString();
obj.charCounter();
}
}
class EvenCharacters {
public void charCounter() {
System.out.println("Strings with even number of characters:");
for (int i = 0; i < 10; i++) {
this.char_count = 0;
for(int j =0; j<this.message[i].length();j++) {
if(this.message[i].charAt(j) == ' ')
continue;
else
this.char_count++;
}
if (this.char_count%2 == 0)
System.out.println(this.message[i]);
}
}
public void inputString() {
Scanner in = new Scanner(System.in);
System.out.println("Enter the strings one by one:");
for (int i = 0; i < 10; i++) {
System.out.println("Enter a string:");
this.message[i] = in.nextLine();
}
in.close();
}
EvenCharacters() {
this.message = new String[10];
this.char_count = 0;
}
private int char_count;
private String[] message;
}
No comments:
Post a Comment