Using java built-in methods we can slove different java string related programs. Most of the difficult java programs can solve by those methods. Our duty is to arrange the methods properly
import java.util.*;
public class Initial1
{
public static void main(String []args)
{
String str,res="";
System.out.print("Enter A String : ");
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
str=str.trim().toLowerCase();
String []word=str.split(" ");
for(int i=0;i<word.length;i++)
{
res += String.valueOf(word[i].charAt(0)).toUpperCase();
res += ".";
}
res=res.substring(0,res.length()-1);
System.out.print("Result is " + res.trim());
}
}
Output:
Enter A String: rabinDra NatH taGOre.
Result is R.N.T
import java.util.*;
public class Initial2
{
public static void main(String []args)
{
String str,res="";
int i;
System.out.print("Enter A String : ");
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
str=str.trim().toLowerCase();
String []word=str.split(" ");
for(i=0;i<word.length-1;i++)
{
if(word[i].length()>1)
{
res += String.valueOf(word[i].charAt(0)).toUpperCase();
}
else
{
res += String.valueOf(word[i].charAt(0)).toUpperCase();
}
res += ".";
}
res += String.valueOf(word[i].charAt(0)).toUpperCase() + word[i].substring(1);
System.out.print("Result is " + res.trim());
}
}
Output:
Enter A String : rabinDra NatH taGOre.
Result is R.N.Tagore.
import java.util.*;
public class ProperCase
{
public static void main(String []args)
{
String str,res="";
System.out.print("Enter A String : ");
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
str=str.trim().toLowerCase();
String []word=str.split(" ");
for(int i=0;i<word.length;i++)
{
if(word[i].length()>1)
{
res += String.valueOf( word[i].charAt(0)).toUpperCase() + word[i].substring(1);
}
else
{
res += String.valueOf(word[i].charAt(0)).toUpperCase();
}
res += " ";
}
System.out.print("Result is " + res.trim());
}
}
Output:
Enter A String : rabinDra NatH taGOre.
Result is Rabindra Nath Tagore.
import java.util.*;
public class Reverse1
{
public static void main(String []args)
{
String str,res="";
int i;
System.out.print("Enter A String : ");
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
StringBuffer sb=new StringBuffer(str);
res=sb.reverse().toString();
System.out.print("Result is " + res);
}
}
Output:
Enter A String : Rabindra Nath Tagore
Result is erogaT htaN ardnibaR
import java.util.*;
public class Reverse2
{
public static void main(String []args)
{
String str,res="";
System.out.print("Enter A String : ");
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
StringBuffer sb;
String []word=str.split(" ");
for(int i=0;i<word.length;i++)
{
if(word[i].length()>1)
{
sb=new StringBuffer(word[i]);
res +=sb.reverse().toString();
}
else
{
res += word[i];
}
res += " ";
}
System.out.print("Result is " + res.trim());
}
}
Output:
Enter A String : Rabindra Nath Tagore
Result is ardnibaR htaN erogaT
import java.util.*;
public class SurnameName
{
public static void main(String []args)
{
String str,res="";
System.out.print("Enter A String : ");
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
StringBuffer sb;
String []word=str.split(" ");
res = word[word.length-1] + " ";
for(int i=0;i<word.length-1;i++)
{
res += word[i] + " ";
}
System.out.print("Result is " + res.trim());
}
}
Output:
Enter A String : Rabindra Nath Tagore
Result is Tagore Rabindra Nath
import java.util.*;
public class UrlTrim
{
public static void main(String []args)
{
String str,res="";
System.out.print("Enter A String : ");
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
int l=str.lastIndexOf("/");
res=str.substring(l+1);
res=res.substring(0,res.indexOf("."));
System.out.print("Result is " + res);
}
}
Output:
Enter A String : https://tutorialathome.in/java/java-string-programs.html
Result is java-string-programs
import java.util.*;
public class StringTest
{
public static void main(String []args)
{
String str;
int uc=0,lc=0,di=0,sp=0;
System.out.print("Enter A String : ");
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
for(int i=0;i<str.length();i++)
{
if(Character.isDigit(str.charAt(i)))
{
di++;
}
else if(Character.isLetter(str.charAt(i)))
{
if(Character.isUpperCase(str.charAt(i)))
{
uc++;
}
else
{
lc++;
}
}
else if(Character.isWhitespace(str.charAt(i)))
{
sp++;
}
}
System.out.println("Frequecy Of Upper case Character : " + uc);
System.out.println("Frequecy Of Lower case Character : " + lc);
System.out.println("Frequecy Of Digit : " + di);
System.out.println("Frequecy Of Space : " + sp);
}
}
Enter A String : Ram is a Good Boy in School. His roll No is 13.
Frequecy Of Upper case Character : 6
Frequecy Of Lower case Character : 26
Frequecy Of Digit : 2
Frequecy Of Space : 11