String programs:- 

  1. 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

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here