Intriguing String Programs part-1
for more string programs https://wordpress-343193-1101484.cloudwaysapps.com/count-words-start-and-end-with-a-vowel
In this post, we will discuss some interesting String programs
for more string programs
An excellent String program to count words start and end with a vowel
- To Reverse the String:-
import java.io.*;
class Strings1
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Give a string");
String str=br.readLine();
int len=str.length();
String rev="";
for(int i=len-1;i>=0;i--)
rev+=str.charAt(i);
System.out.println("The reversed string is "+rev);
}
}
Input:- Computer
Output:- retupmoC
2. Palindrome String:-
import java.io.*;
class Palindrome
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Give a string");
String str=br.readLine();
int len=str.length();
String rev="";
for(int i=len-1;i>=0;i--)
rev+=str.charAt(i);
if(str.equals(rev))
System.out.println("It is a palindrome string");
else
System.out.println("It is not a palindrome string");
}
}
Input:- MoM
Output:- It is a palindrome string
Input:- papa
Output:- It is a not a palindrome string
3. To print the initials of a name:-
import java.io.*;
class Initials
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Give a string");
String str=br.readLine();
char c=str.charAt(0);
String str1=" ";
str1+=c + ".";
int a=str.indexOf(' ');
char d=str.charAt(a+1);
String str2=" ";
str2=d+ ".";
str1=str1+str2;
int b=str.lastIndexOf(' ');
str2=str.substring(b+1);
str1=str1+str2;
System.out.println("Your name is : "+str);
System.out.println("Your initials are: "+str1);
}
}
Input:- Mohandas Karamchand Gandhi
Output:- M.K.Gandhi
4. To calculate number of spaces, words, vowels and consonants from a string:-
import java.io.*;
class VowelCons
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Give a string");
String str=br.readLine();
int len=str.length();
int count=0;
int vow=0;
for(int i=0;i<len;i++)
{
char ch=str.charAt(i);
if(ch==' ')
count++;
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
vow++;
}
System.out.println("No.of spaces= "+count);
System.out.println("No.of words= "+(count+1));
System.out.println("No.of vowels= "+vow);
int j=len-count-vow;
System.out.println("No.of consonants= "+j);
}
}
Output:-
Give a string
What is your name?
No.of spaces= 3
No.of words= 4
No.of vowels= 6
No.of consonants= 9
For videos related to java programming
http://www.youtube.com/user/trushnatej
Related Posts
Continue learning with these related articles

String Methods in Java for ICSE Class 10: 25 Output Questions with Answers
ICSE Class 10 Computer Applications practice on String class methods: length, charAt, indexOf, lastIndexOf, substring, replace, trim, compareTo, equals and more, with 25 board-style output questions and step-by-step answers.

Special Number Programs in Java for ICSE: Armstrong, Spy, Neon, Disarium, Pronic and More
Every special number asked in ICSE Computer Applications – prime, perfect, Armstrong, palindrome, automorphic, neon, spy, Niven, duck, buzz, Disarium, special and pronic numbers – with definitions, examples and one complete Java program that checks them all.

Math Library Methods in Java for ICSE Class 9 (with Outputs)
ICSE Class 9 Computer Applications notes on the Java Math class: sqrt, cbrt, pow, abs, max, min, ceil, floor, round, rint and random, with return types, tricky negative-number outputs and Java expressions for formulas.