Java String Programs - Part 1
28-07-2017    787 times
Java String Programs

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 






Write a program to accept a name from the user and print the initial character of the name. For example, if the given name is "rabinDra NatH taGOre. Then the o, tput will be R.N.T

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

Write a program to accept a name from the user and print the initial character of the name with surname. For example if the given name is "rabinDra NatH taGOre. Then the output will be R.N.Tagore

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.

Write a program to accept a name from the user and print the name in title case or proper case format. For example, if the given name is "rabinDra NatH taGOre. Then the output will be Rabindra Nath 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.

Write a program to accept a name from the user and reverse it, if the given name is "Rabindra Nath Tagore". Then the output will be erogaThtaNardnibaR

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

Write a program to accept a name from the user and reverse it, if the given name is "Rabindra Nath Tagore". Then the output will be ardnibaR htaN erogaT.

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 

Write a program to accept a name from the user and reverse it, if the given name is "Rabindra Nath Tagore". Then the output will be Tagore Rabindra Nath.

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 

Write a program to accept a url from the user and extract the file name without extension.

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

Write a program to accept a string from the user and find out the following statistics:

  • Count of upper case character
  • Count of lower case character
  • Count of digits
  • Count of spaces

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

Output:

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

softetechnologies
    Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
    Related Post
  Query About the post