Bubble Sort & Char and String Array
Bubble Sort & Char and String Array
Sorting refers to the operation or technique of rearranging sets of data in either ascending or descending
order.
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 1
// Write a program to bubble sort an integer array whose elements are 5,10,20,4 and 3
// Sort in ascending order.
import java.util.*;
class bubsortint
{
public static void main()
{
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 2
// Write a program to bubble sort an integer array whose elements are 5,10,20,4 and 3
// Sort in descending order.
import java.util.*;
class bubsortint
{
public static void main()
{
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 3
// Write a program to bubble sort a character array whose values are 'z','e','r' and 'b'
// Sort in ascending order
import java.util.*;
class bubsortchar
{
public static void main()
{
Scanner inp = new Scanner(System.in);
char ar[]={'z','e','r','b'};
int x=ar.length;
char temp;
System.out.println("Original array");
for(int i=0;i<x-1;i++)
{
System.out.println(ar[i]);
}
for(int i=0;i<x-1-i;i++)
{
for(int j=0;j<x-1-i;j++)
{
if((int)ar[j]>(int)ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
System.out.println("Sorted array");
for(int i=0;i<x;i++)
{
System.out.print(ar[i]+" ");
}
}
}
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 4
// Write a program to bubble sort a string array whose values are "Delhi","Chennai","Mumbai" and
// "Bangalore" .Sort in ascending order
class bubblestring
{
public static void main()
{
String a[ ]={"Delhi","Chennai","Mumbai","Bangalore"};
int x=a.length;
String temp;
System.out.println("Original array");
for(int i=0;i<x-1;i++)
{
System.out.println(a[i]);
}
for(int i=0;i<x-1;i++)
{
for(int j=0;j<(x-1)-i;j++)
{
if(a[j].compareTo(a[j+1])>0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Sorted array");
for(int i=0;i<x;i++)
{
System.out.println(a[i]+" ");
}
}
}
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 5
Sorting involving two arrays
//Program to accept n number of cities and their respective temperature.
//Sort the arrays with respect to the temperature in ascending order
//Display the sorted list.
import java.util.*;
class citytemp
{
public static void main()
{
String t2; int t1;
Scanner inp = new Scanner(System.in);
System.out.print("Enter the size of the array ");
int n=inp.nextInt();
String city[ ]=new String[n];
int ar[]=new int[n];
int x=ar.length;
String dummy;
for(int i=0;i<x;i++)
{
dummy=inp.nextLine();
System.out.print("Enter the name of the city ");
city[i]=inp.nextLine();
System.out.print("Enter the temperature of the city ");
ar[i]=inp.nextInt();
}
for(int i=0;i<x-1;i++)
{
for(int j=0;j<x-1-i;j++)
{
if(ar[j]>ar[j+1])
{
t1=ar[j];
ar[j]=ar[j+1];
ar[j+1]=t1;
t2=city[j];
city[j]=city[j+1];
city[j+1]=t2;
}
}
}
System.out.println("Temperature City");
for(int i=0;i<x;i++)
{
System.out.println(ar[i]+"\t\t"+city[i]);
} } }
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 6
String and char Array Programs using Library functions(String functions and Character Functions)
Define a class to declare a character array of size ten, accept the character into the array and perform the
following:
Count the number of uppercase letters in the array and print.
Count the number of vowels in the array and print.
import java.util.*;
class chararacter_array_program
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
System.out.println("enter 10 characters”);
char arr[]=new char[10];
for(int i=0; i<arr.length; i++)
{ arr[i]= sc.next().charAt(0); }
int uc=0;
int vowels=0;
for(int i=0;i<arr.length;i++)
{
char c = arr[i];
if(Character.isUpperCase(c))
uc++;
if(“aeiouAEIOU”.indexOf( c )!=-1)
vowels++;
}
System.out.println(Number of uppercase letters “ + uc);
System.out.println(Number of vowels “ + vowels);
}
}
Define a class to accept and store 10 strings into the array and print the strings with even number of
characters.
import java.util.*;
class String_array_program
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
System.out.println("enter 10 Strings”);
String arr[]=new String[10];
for(int i=0; i<arr.length; i++)
{
arr[i]= sc.next();
}
System.out.println(“Strings with even number of characters”);
for(int i=0;i<arr.length;i++)
{
int len=arr[i].length();
If(len % 2 == 0)
System.out.println(arr[i]);
}
} }
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 7
Define a class to accept and store 10 strings into the array and print the strings with more than 5 characters
and whose suffix is “ing”.
//Define a class to accept and store 10 strings into the array and
//print the strings with more than 5 characters and whose suffix is “ing”.
import java.util.*;
class String_array_program
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
System.out.println("enter 10 Strings");
String arr[]=new String[10];
for(int i=0; i<arr.length; i++)
{
arr[i]= sc.next();
}
System.out.println("Strings with more than 5 characters and suffix ing");
for(int i=0;i<arr.length;i++)
{
int len=arr[i].length();
if(len >5 && arr[i].endsWith("ing"))
System.out.println(arr[i]);
}
} }
Define a class to declare a character array of size n, accept the character into the array and perform the
following:
• Count the number of characters whose ascii value is greater than 65 and print.
• Count the number of vowels in the array and print.
import java.util.*;
class chararacter_array_program
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of n");
int n=sc.nextInt();
System.out.println("enter 10 characters");
char arr[]=new char[n];
for(int i=0; i<arr.length; i++)
{ arr[i]= sc.next().charAt(0); }
int ascii=0;
int vowels=0;
System.out.println("Characters that are vowels");
for(int i=0;i<arr.length;i++)
{
char c = arr[i];
if("aeiouAeiou".indexOf(c)!=-1)
{
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 8
System.out.println(c);
vowels++;
}
}
System.out.println("Number of vowels "+ vowels);
Define a class to declare a character array of size ten, accept the character into the array and perform the
following:
Count the number of digits and alphabets in the array and print the same.
import java.util.*;
class chararacter_array_program
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
char arr[]=new char[10];
System.out.println("enter 10 characters");
for(int i=0; i<arr.length; i++)
{ arr[i]= sc.next().charAt(0); }
int digits=0;
int alphs=0;
System.out.println("Characters that are vowels");
for(int i=0;i<arr.length;i++)
{
char c = arr[i];
if(Character.isDigit(c))
{
digits++;
}
else if(Character.isLetter(c))
{
alphs++;
}
}
System.out.println("Number of Digits "+ digits);
System.out.println("Number of alphabets "+alphs);
}
}
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 9
Define a class to declare a String array of size ten, accept Strings into the array and perform the following:
i. Display the strings that start with a vowel
ii. Display the count of the strings that start with a vowel
import java.util.*;
class String_array_program
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
System.out.println("enter 10 Strings");
String arr[]=new String[10];
for(int i=0; i<arr.length; i++)
{
arr[i]= sc.next();
}
int ctr=0;
System.out.println("Strings that start with vowel");
for(int i=0;i<arr.length;i++)
{
char ch=arr[i].charAt(0);
if("aeiouAeiou".indexOf(ch)!=-1)
{
System.out.println(arr[i]);
ctr++;
}
}
System.out.println("No. of Strings that start with vowel :"+ctr);
}
}
Define a class to declare a String array of size n, accept names of the students into the array and perform
the following:
i. Display the name with the maximum length
ii. Display the name which is alphabetically greatest
import java.util.*;
class String_alphagreat_lengthgreat
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the value of n");
int n=sc.nextInt();
String arr[]=new String[n];
System.out.println("enter "+ n+" Strings");
for(int i=0; i<arr.length; i++)
{
arr[i]= sc.next();
}
String maxlength=arr[0],maxalpha=arr[0];
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 10
for(int i=0;i<arr.length;i++)
{
if(arr[i].length() > maxlength.length())
maxlength=arr[i];
if(arr[i].compareTo(maxalpha)>0)
maxalpha=arr[i];
}
System.out.println("Longest name " +maxlength);
}
}
Bubble Sort & String and char Array Programs using Library functions [Celine Biju] Page 11