String Programs

Accept the 20 Name using String array and sort in ascending order [ CISCE -2015 Board Exam]

import java.util.*;


public class StringSortingUsingBubbleSort

{

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);

String name[]= new String[20];

System.out.println(" Enter the " +name.length+ "\t Names");

for(int i=0; i<name.length; i++)

{

name[i]=sc.next();

}

BubbleSort(name);

//Begin For loop

System.out.println(" Sorted " +name.length+ "\t Names");

for(int i=0; i<name.length; i++)

{

System.out.println(name[i]);

}

}// End of Main

private static void BubbleSort(String[] array) {

String temp;

// Begin For loop

for(int i=0; i<array.length; i++) {

//Open For loop


for(int j=0; j<array.length-1-i; j++) {


//Open If Statement Compare and sort strings

if(array[j].compareTo(array[j+1])>0) {

temp = array[j];

array[j] = array[j+1];

array[j+1] = temp;


}//End of If Statement


}//End of For Loop

}//End of For Loop

}//End of Private Class BubbleSort


}


Program of String Sorting without using Functions ( in Ascending order. . )


import java.util.*;


public class StringSortingUsingBubbleSort

{

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);

String name[]= new String[5];

System.out.println(" Enter the " +name.length+ "\t Names");

for(int i=0; i<name.length; i++)

{

name[i]=sc.next();

}

// Printing the Name without sorting

System.out.println(" UnSorted " +name.length+ "\t Names");

for(int i=0; i<name.length; i++)

{

System.out.println(name[i]);

}

String temp;

for(int i=0; i<name.length; i++)

{

for(int j=0; j<name.length-1-i; j++)

{


//Open If Statement Compare and sort strings

if(name[j].compareTo(name[j+1])>0) // for Descending oder make it <0)

{

temp = name[j];

name[j] = name[j+1];

name[j+1] = temp;


}//End of If Statement


}

}

System.out.println(" Sorted " +name.length+ "\t Names");

for(int i=0; i<name.length; i++)

{

System.out.println(name[i]);

}

}

}


Java program to check the Palindrome String

import java.util.*;

class PalindromeString

{

public static void main(String args[])

{

String str, reverse = "";

Scanner sc = new Scanner(System.in);

System.out.println("Enter a String ");

str = sc.next();

int length = str.length();

for (int i = length - 1; i >= 0; i--)

{

reverse = reverse + str.charAt(i);

}

if (str.equals(reverse))

System.out.println("Its palindrome.");

else

System.out.println(" Its not palindrome.");

}

}

Java Program to Reverse a String

public class Reverse

{

public static void main(String[] args) {

String sIn="sharjah dubai";

String sOut="";

char n[]=new char[sIn.length()];

for (int i = 0; i < sIn.length(); i++) {

n[i]=sIn.charAt(i);

}

for (int i = n.length-1; i >=0; i--) {

String s=String.valueOf(n[i]);

sOut=sOut.concat(s);

}

System.out.println(sOut);

}

}


Java program to change all vowels in lowercase and consonants in uppercase

public class StringProgram

{

public static void main(String[] args)

{

java.util.Scanner scn= new java.util.Scanner(System.in);

System.out.println(" Enter the String");

String s1=scn.nextLine();

String s2=s1.toUpperCase();

s2=s2.replace('A', 'a');

s2 =s2.replace('E', 'e');

s2 =s2.replace('I', 'i');

s2 =s2.replace('O', 'o');

s2 =s2.replace('U', 'u');

System.out.println(s2);

}

}