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 = “Computer” ch = ‘e’
Output: number of e present is=1
ii) void check (String s1) – to display only the vowels from string s1 , after converting it to lower case.
Example :Input:S1= “Vidyanagar”
Output: i a a a
Program
class Str
{
public void check(String str, char ch)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
{
char Char = str.charAt(i);
if (ch == Char)
{
count++;
}
}
System.out.println("number of "+ch+ " present is = " + count);
}
public void check(String s1)
{
s1 = s1.toLowerCase();
for (int i = 0; i < s1.length(); i++)
{
char Char = s1.charAt(i);
if (Char == 'a' || Char == 'e' || Char == 'i' ||Char == 'o'
|| Char == 'u') {
System.out.print(Char + " " );
}
}
}
public static void main(String args[])
{
Str ob = new Str();
ob.check("Computer",'e');
ob.check("Vidyanagar");
}
}
No comments:
Post a Comment