(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
import java.util.*;
class Ab
{
String str1;
int i,len,choice;
char ch;
Scanner sc=new Scanner(System.in);
public void take()
{
System.out.print("\nEnter your choice (1 for checking a character in a sentence, 2 for displaying vowels:");// "THIS IS A TEST OF ALL A'S "
choice=sc.nextInt();
sc=new Scanner(System.in);
if(choice==1)
{
System.out.print("\nEnter sentence:");
str1=sc.nextLine().toLowerCase();
System.out.print("\nEnter character:");
ch=sc.next().charAt(0);
check(str1,ch);
}
else if(choice==2)
{
System.out.print("\nEnter sentence:");
str1=sc.nextLine().toLowerCase();
check(str1);
}
else
System.out.print("\nWrong Choice...");
}
private void check(String s, char ch)
{
int i,len,c=0;
len=s.length();
for(i=0;i<len;i++)
{
if(s.charAt(i)==ch)
c++;
}
System.out.print("\nCharacter '"+ ch+ "' is present for "+ c+ " times is sentence:"+s);
}
private void check(String s)
{
int i,len;
char ch;
len=s.length();
System.out.print("\nThe Vowels are as follows: ");
for(i=0;i<len;i++)
{
ch=s.charAt(i);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.print(" "+ch);
}
}
}
}
for(i=0;i<len;i++)
{
ch=s.charAt(i);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.print(" "+ch);
}
}
}
}
No comments:
Post a Comment