0% found this document useful (0 votes)
4 views3 pages

String Programs in Oops

The document contains three Java programs: one that counts occurrences of a specified character in a given text, another that identifies and counts vowels and consonants in a string, and a third that extracts a substring based on user-defined indices. Each program utilizes the Scanner class for user input and demonstrates string manipulation methods such as indexOf and substring. The code snippets are complete and ready for execution.

Uploaded by

ksravanthi950
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

String Programs in Oops

The document contains three Java programs: one that counts occurrences of a specified character in a given text, another that identifies and counts vowels and consonants in a string, and a third that extracts a substring based on user-defined indices. Each program utilizes the Scanner class for user input and demonstrates string manipulation methods such as indexOf and substring. The code snippets are complete and ready for execution.

Uploaded by

ksravanthi950
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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);
}
}

You might also like