InterviewSolution
| 1. |
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: str = “success” ch = ‘s’ . Output: number of s present is = 3(ii) void check(String si) – to display only vowels from string si, after converting it to lower case.Example:Input:s1 = “computer”Output:o u e |
|
Answer» (i) class CharacterVowel { public void checkering str, char ch) { int c = 0, code,i,s; str = str.toLowerCase( ); int len = str.length( ); for (code = 97; code < 122; code+ +) { c = 0; for (i = 0; i < len; i+ +) { ch = str.charAt(i); s = (int) ch; if(s = = code) c = c + 1; } ch = (char)code; if(c ! = 0) System.out.println(“Frequency of “+ch+ “is” +c); } } (ii) public void check(String si) { int i; char ch=0, chr=0; for(i=0;i<s1. length(); i+ +) { ch = s1.charAt(i); if(Character.isUpperCase(ch)) chr = Character. toLo werCase(ch); if((s1 .charAt(i)==’a’) 11 (s1 .charAt(i) = ‘u’) | j(s1 .charAt(i) = = ‘o’)| | (s1 . charAt(i)==’i’) 11 (s1 .charAt(i) = = ‘e’)) System.out.println(s1 .charAt(i)); } } } |
|