Sorting String and char, string pattern
Sorting String and char, string pattern
Write a program to store 10 name/words in an array. Sort them in ascending order using
exchange selection sort.
import java.util.*;
class Exchange
{
public void sort()
{
Scanner sc= new Scanner(System.in);
String names[]= new names[10];
String temp= “”, small= “”;
int p=0;
//storing elements in the array
for(inti=0;i<10;i++)
{
System.out.println(“Enter a name/word:”);
name[i]=sc.next();
}
// Sorting
for(inti=0;i<9;i++)
{
small=names[i];
p=i;
for(int j=i+1;j<10;j++)
{
if(names[j].compareTo(small)<0)
{
small = names[j];
p=j;
}
}
temp=names[j];
names[j]=names[p];
names[p]=temp;
}
//printing the sorted array
for(inti=0;i<10;i++)
{
System.out.println(names[i]);
}
}
}
Question 2:
Write a program to store 10 name/words in an array. Sort them in descending order using
exchange selection sort.
import java.util.*;
class Exchange
{
public void sort()
{
Scanner sc= new Scanner(System.in);
String names[]= new names[10];
String temp= “”, max= “”;
int p=0;
//storing elements in the array
for(inti=0;i<10;i++)
{
System.out.println(“Enter a name/word:”);
name[i]=sc.next();
}
// Sorting
for(inti=0;i<9;i++)
{
max=names[i];
p=i;
for(int j=i+1;j<10;j++)
{
if(names[j].compareTo(max)>0)
{
max= names[j];
p=j;
}
}
temp=names[j];
names[j]=names[p];
names[p]=temp;
}
//printing the sorted array
for(inti=0;i<10;i++)
{
System.out.println(names[i]);
}
}
}
Question 3:
Write a program to store 20characters in an array. Sort them in ascending order using exchange
selection sort.
import java.util.*;
class Exchange
{
public void sort()
{
Scanner sc= new Scanner(System.in);
char ch[]= new char[20];
char temp= “”, small= “”;
int p=0;
//storing elements in the array
for(inti=0;i<10;i++)
{
System.out.println(“Enter a character:”);
ch[i]=sc.next().charAt(0);;
}
// Sorting
for(inti=0;i<9;i++)
{
small=ch[i];
p=i;
for(int j=i+1;j<10;j++)
{
if(ch[j]<small)
{
small =ch[j];
p=j;
}
}
temp=ch[j];
ch[j]=ch[p];
ch[p]=temp;
}
//printing the sorted array
for(inti=0;i<10;i++)
{
System.out.println(ch[i]);
}
}
}
Question 4:
Write a program to store 20 characters in an array. Sort them in descending order using
exchange selection sort.
import java.util.*;
class Exchange
{
public void sort()
{
Scanner sc= new Scanner(System.in);
char ch[]= new char[20];
char temp= “”, max= “”;
int p=0;
//storing elements in the array
for(inti=0;i<10;i++)
{
System.out.println(“Enter a character:”);
ch[i]=sc.next().charAt(0);;
}
// Sorting
for(inti=0;i<9;i++)
{
max=ch[i];
p=i;
for(int j=i+1;j<10;j++)
{
if(ch[j]>max)
{
max=ch[j];
p=j;
}
}
temp=ch[j];
ch[j]=ch[p];
ch[p]=temp;
}
//printing the sorted array
for(inti=0;i<10;i++)
{
System.out.println(ch[i]);
}
}
}
Question 5:
Write a program to accept a word, convert it into capital letter and print the following pattern:
Example : WATER
W
WA
WAT
WATE
WATER
import java.util.*;
class Pattern
{
public void print()
{
Scanner sc= new Scanner(System.in);
System.out.println(“Enter a word:”);
String wrd=sc.next();
wrd=wrd.toUpperCase();
int l=wrd.length();
for(int i=0;i<l;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(wrd.charAt(j));
}
}
}
}
Question 6:
Write a program to accept a word, convert it into capital letter and print the following pattern:
Example : WATER
WATER
WATE
WAT
WA
W
import java.util.*;
class Pattern
{
public void print()
{
Scanner sc= new Scanner(System.in);
System.out.println(“Enter a word:”);
String wrd=sc.next();
wrd=wrd.toUpperCase();
int l=wrd.length();
for(int i=l-1;i>=0;i--)
{
for(int j=0;j<=i;j++)
{
System.out.print(wrd.charAt(j));
}
}
}
}
Question 7:
Write a program to store 10 words or names in an array. Display the names which begin with A.
import java.util.*;
class Names
{
public void print()
{
Scanner sc= new Scanner(System.in);
String names[]= new String[10];
for(int i=0;i<10;i++)
{
System.out.println(“Enter a word or name:”);
names[i]=sc.next();
}
System.out.println(“Names begin with A:”);
for(int i=0;i<10;i++)
{
if(names[i].charAt(0)==’A’)
{
System.out.println(names[i]);
}
}
}
}
Question 8:
Write a program to store 10 words or names in an array. Display the names which begin and
end with vowel.
import java.util.*;
class Names
{
public void print()
{
Scanner sc= new Scanner(System.in);
String names[]= new String[10];
for(int i=0;i<10;i++)
{
System.out.println(“Enter a word or name:”);
names[i]=sc.next();
}
System.out.println(“Names begin and end with vowel:”);
char ch1= ‘ ’, ch2= ‘ ’;
for(int i=0;i<10;i++)
{
ch1=names[i].charAt(0);
ch2=names[i].charAt(names[i].length()-1)
if((ch1== ‘A|| ch1==’a’ ||ch1==’E’ ||ch1==’e’ ||ch1==’I’ ||ch1==’i’ ||ch1==’O’
||ch1==’o’ ||ch1==’U’ ||ch1==’u’) &&
(ch2==’A’||ch2==’a’ ||ch2==’E’
||ch2==’e’ ||ch2==’I’ ||ch2==’i’ ||ch2==’O’ ||ch2==’o’ ||ch2==’U’ ||ch2==’u’ ))
{
System.out.println(names[i]);
}
}
}
}
Question 9:
1. Calculating number of rows and columns of a matrix.
We can find the number of rows in a matrix A[][] using A.length. To find the number of
columns A[i].length or A[0].length