You are on page 1of 6

Assignment # 04

Q1: Create a String array. Fill it and Display the contents of array in Ascending
Order

import java.util.Scanner;
class string ascending {
public static void main(String args[]){
String temp, s[] ={"anzala","tayyaba","osama","razzan","daniyal","manal","iqra"};
System.out.println("\nThe names in original order are:");
for(int i=0;i<s.length;i++)
System.out.println(s[i]);
for(int i=0;i<s.length;i++)
{
for(int j=i+1;j<s.length;j++)
{
if(s[i].compareTo(s[j])>0)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
System.out.println("\nThe given names in alphabetical order are:");
for(int i=0;i<s.length;i++)
System.out.println(s[i]);
}
}

Q2: Create an int aray of size 10 and initialize it with Random even numbers
from 22 to 40. Display the contents of array.

import java.lang.Math;
class randomeven
{
public static void main(String args[])
{
int x, a[] = new int [10];
for(int i=0;i<10;i++)
{
a[i]= (int) (Math.random()* ((40 - 22)+1))+22;
x=a[i]%2;
if(x==1)
i--;
}
for(int i=0;i<10;i++)
{
System.out.println(a[i]);
}
}
}

Q3: Generate 100 Random characters and checks every character for a vowel. If
it is a vowel print it.

import java.lang.Math;
class randomchar
{
public static void main(String args[])
{
char a[] = new char [100];
for(int i=0;i<100;i++)
{
a[i]= (char) (Math.random()*((122-97)+1)+97);
}
System.out.println("\nThe vovels in the generated characters are:");
for(int i=0;i<100;i++)
{
if(a[i]=='a' || a[i]=='e' || a[i]=='i' || a[i]=='o' || a[i]=='u')
System.out.println(a[i]);
}
}
}
Q4: Write a Program that can fill and array of int with prime numbers less than
100.

import java.util.Scanner;
class primenumberarray
{
public static void main(String args[])
{
int y,b,a,z=0,i;
for(a=2;a<=100;a++)
{
for(b=a-1;b>=2;b--)
{
y=a%b;
if(y==0)
break;
}
if(b==1)
z++;
}
System.out.println("\nThe no. of prime numbers from 0 to 100 is "+z+"\nAnd
those prime numbers are:");
int p[]= new int [z];
i=0;
for(a=2;a<=100;a++)
{
for(b=a-1;b>=2;b--)
{
y=a%b;
if(y==0)
break;
}
if(b==1)
{
p[i]=a;
i++;
}
}
for(i=0;i<z;i++)
System.out.println(p[i]);
}
}

Q5: Create a String variable with a Paragraph. Create a String Array equal to the
numbers of word in the Paragraph. Fill the Array with the words and sort them
in Ascending Order. The program must be dynamic i.e. change in the Paragraph
will not effect the Program.

import java.util.Scanner;
class parastring
{
public static void main(String args[])
{
Scanner sn=new Scanner(System.in);
System.out.print("\nEnter a paragraph: ");
String para = sn.nextLine();
char ch [] = para.toCharArray();
int i,space=0;
for(i=0;i<ch.length;i++)
{
if(Character.isWhitespace(ch[i]) || ch[i]==',' || ch[i]=='.')
space++;
}
int j=0, ind[] = new int [space];
for(i=0;i<ch.length;i++)
{
if(Character.isWhitespace(ch[i]) || ch[i]==',' || ch[i]=='.')
{
ind[j]=i;
j++;
}
}
String temp, s[] = new String [space+1];
j=0;
s[0] = para.substring(0,ind[j]);
for(i=1;i<s.length-1;i++)
{
s[i] = para.substring(ind[j]+1,ind[j+1]);
j++;
}
s[i] = para.substring(ind[j]+1);
for(i=0;i<s.length;i++)
{
for(j=i+1;j<s.length;j++)
{
if(s[i].compareToIgnoreCase(s[j])>0)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
System.out.println("\nThe words of the given paragraph in alphabetical order
are:");
for(i=0;i<s.length;i++)
{
if(s[i].length()>0)
System.out.println(s[i]);
}
}
}

Q6: Create a String variable with a Paragraph. Check the number of characters in
the String and count the occurrence of each character.

java.util.Scanner;
class charcount
{
public static void main(String args[])
{
Scanner sn=new Scanner(System.in);
System.out.print("\nEnter a paragraph: ");
String para = sn.nextLine();
char l, ch [] = para.toCharArray();
int i,space=0,alp []=new int [26];
for(i=0;i<ch.length;i++)
{
for(l='a';l<='z';l++)
{
if(ch[i]==l)
alp[(int)l-97]++;
}
if(Character.isWhitespace(ch[i]))
space++;
}
System.out.println("\nThe total no. of characters in the string '"+para+"' are "+
(ch.length-space));
System.out.println("And the no. of occurrence of each character is given as
follows:");
for(l='a';l<='z';l++)
{
System.out.println(l+" = "+alp[(int)l-97]);
}
System.out.println("Spaces = "+space);
}
}

You might also like