Program to Sort the characters of a string
package selenium3;
import java.util.Arrays;
import java.util.Scanner;
public class CharAlphabeticOrder {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
try{
System.out.println("Enter the string");
String s = in.nextLine();
char[] chars = s.toCharArray();
Arrays.sort(chars);
System.out.print("String in sorted order:");
for(int i=chars.length-1;i>=0;i--)
{
System.out.print(chars[i]);
}
}
catch(Exception e){
System.out.println(e);
}
finally
{
in.close();
}
}
}
Program to sort the strings in alphabetical order
package selenium3;
import java.util.Scanner;
public class StrAlphabeticOrder {
public static void main(String[] args) throws Exception {
int n;
String temp;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of strings");
n = in.nextInt();
String names[]=new String[n];
Scanner s1 = new Scanner(System.in);
try{
for(int i=0; i<n; i++)
{
names[i]=s1.nextLine();
}
for(int i=1;i<=n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(names[i].compareTo(names[j])>0)
{
temp=names[i];
names[i]=names[j];
names[j]=temp;
}
}
}
System.out.print("Names in sorted order:");
for(int i=0;i<n-1;i++)
{
System.out.print(names[i]+",");
}
System.out.print(names[n-1]);
}
catch(Exception e){
System.out.println(e);
}
finally
{
s1.close();
in.close();
}
}
}
Program to reverse the words of a string separately
package selenium3;
import java.util.Scanner;
public class ReverseStringWords {
public static String reverseword(String str)
{
String words[]=str.split("\\s");
String revWord = " ";
for(String w:words)
{
StringBuilder sb = new StringBuilder(w);
sb.reverse();
revWord=revWord+sb.toString()+" ";
}
return revWord.trim();
}
public static void main(String[] args) throws Exception{
Scanner in = new Scanner(System.in);
try
{
System.out.println("Enter the string");
String st = in.nextLine();
String rev = reverseword(st);
System.out.println(rev);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
in.close();
}
}
}
Program to count the occurrence of each character in a string
package selenium3;
import java.util.HashMap;
import java.util.Scanner;
public class CharCount {
public static void main(String[] args) throws Exception{
Scanner in = new Scanner(System.in);
try{
System.out.println("Enter the string");
String str = in.nextLine();
HashMap<Character,Integer> map = new HashMap<>();
for(char ch:str.toCharArray())
{
if(map.containsKey(ch))
{
int val=map.get(ch);
map.put(ch,val+1);
}
else
{
map.put(ch,1);
}
}
System.out.println(map);
}
catch(Exception e){
System.out.println(e);
}
finally
{
in.close();
}
}
}
Program to read a text file and print in console
package selenium3;
import java.io.FileReader;
public class ReadFile
{
public static void main(String[] args)throws Exception
{
FileReader fr = new FileReader("C:/Users/Pc/Desktop/ust.txt");
int i;
while((i=fr.read())!=-1)
{
System.out.print((char)i);
}
}
}
Program to read and print the string from console
package selenium3;
import java.io.*;
public class ReadingString
{
public static void main(String[] args) {
//String newLine = System.getProperty("line.separator");
System.out.println("Reading Strings from console");
//You use System.in to get the Strings entered in console by user
try
{
//You need to create BufferedReader which has System.in to get user input
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String userInput;
System.out.println("Enter text...");
System.out.println("Press Enter to quit.");
do {
//reader .readLine() would wait for the user to enter strings
//keep and entering text and when you press enter, all those would be
displayed
//so it is keeping it in buffer untill the user types 'quit' and presses
enter
userInput = (String)br.readLine();
System.out.println("You entered : " + userInput);
}
while(!userInput.equals("quit"));
}
catch(Exception e)
{
}
}
}
Program to read the string character by character
package selenium3;
import java.io.*;
public class ReadingCharacter {
public static void main(String[] args
//String newLine = System.getProperty("line.separator");
System.out.println("Reading characters from console");
//You use System.in to get the characters entered in console by user
try
{
//You need to create BufferedReader which has System.in to get user input
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
char userInput;
System.out.println("Enter characters...");
System.out.println("Enter 'q' and Press 'Enter' to quit");
do {
//reader .read() would wait for the user to enter character.
//keep and entering characters and when you press enter, all those would be
displayed
//so it is keeping it in buffer until the user types 'q' and presses enter
userInput = (char)br.read();
System.out.println("You entered : " + userInput);
}
while(userInput!= 'q');
}
catch(Exception e)
{
}
}
}
There is integer array. Ex - 122344565888
Print the digit if the immediate digit is also same. o/p- 2 4 8
Class AdjacentDuplicates
{
public static vpod main(String
Scanner in = new Scanner(System.in);
Systemo.out.println("Enter the value");
String input = in.nextLine();
adjacentduplicates(x);
}
public void adjacentDuplicates(int n)
{
LinkedHashMap<character,Integer> map = new LinkedHashMap<character,Integer>();
char[] digits = n.toCharArray();
for(char ch:map){
if(map.containKey(ch)
{
int val=map.get(ch);
val=val+1;
map.put(ch,val);
}
else
{
map.put(ch,1);
}
}
for(Map.Entry entry:map.EntrySet()
{
System.out.println(map.getKey());
}
Write a program to reverse a string word by word.
Ex - String s = this is book
O/p - book is this
Class ReverseWordbyWOrd
{
public static void main(String[] args)
Scanner in = new Scanner(System.in);
Systemo.out.println("Enter the value");
String input =in.nextLine()
reverse(input);
}
public void reverse(String s)
{
String[] words = s.split("\\s");
for(int i=words.length-1; i<=0; i--)
{
System.out.print(words[i]+" ");
}