Week – III Programs
Write an application that uses String method indexOf to determine the total number of
occurrences of any given alphabet in a defined text.
import [Link];
public class IndexOf {
public static void main(String args[]) {
Scanner scan = new Scanner([Link]);
[Link]("Enter a text: ");
String text =[Link]();
[Link]("Enter a character to search: ");
char ch = [Link]().charAt(0);
int count =0;
int index=[Link](ch);
while(index!=-1){
count++;
index=[Link](ch, index+1);
}
[Link]("Total occurances of "+ch+"="+count);
}
}
Write a Java program to print all vowels in given string and count number of vowels and
consonants present in given string
import [Link];
public class Vowelsconsonants {
public static void main(String args[]) {
Scanner scan = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
int vowels=0, consonants =0;
[Link]("Vowels are: ");
for(int i=0; i<[Link](); i++){
char ch = [Link](i);
if(ch>='a' && ch<='z'){
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){
vowels++;
[Link](ch+" ");
}
else
consonants++;
}
}
[Link]("\nTotal vowels: "+vowels);
[Link]("Total consonants: "+consonants);
}
}
Write an application that finds the substring from any given string using substring method
import [Link];
public class SubString {
public static void main(String args[]) {
Scanner scan = new Scanner([Link]);
[Link]("Enter a String: ");
String str = [Link]();
[Link]("Enter starting index: ");
int start = [Link]();
[Link]("Enter ending index: ");
int end = [Link]();
String sub = [Link](start, end);
[Link]("Substring is: "+sub);
}
}