QuesAns Print Page – CSKC
Sign In

Search

siteicon
Q&A ICSE2017-08A #7264

Question:

Design a class to overload a function check( ) as follows:
(i) void check (String str , char ch ) – to find and print the frequency of a character in a string.
Example :
Input: Output:
str = “success” number of s present is =3
ch = ‘s’
[15]
(ii) void check(String s1) – to display only vowels from string s1, after converting it to lower case.
Example :
Input:
s1 =”computer” Output : o u e

Answer:

public class Overload {
 public void check(String str, char ch) {
 int result = 0;
 for (int i = 0; i < str.length(); i++) {
 char currentChar = str.charAt(i);
 if (ch == currentChar) {
 result++;
 }
 }
 System.out.println("number of s present is = " + result);
 }

 public void check(String s1) {
 s1 = s1.toLowerCase();
 for (int i = 0; i < s1.length(); i++) {
 char currentChar = s1.charAt(i);
 if (currentChar == 'a' || currentChar == 'e' || currentChar == 'i' || currentChar == 'o'
 || currentChar == 'u') {
 System.out.print(currentChar + " " );
 }
 }
 }
 }