You are on page 1of 3

Submitted To : Mam Samia Riaz

Submitted By : Danish Ejaz


Registration no : FA21-BSE-074
Section : BSE-3B
Department : Software Engineering

Question no 1:
Write program to input a String from user. Count the number of words in this
string and also display third word.
Answer:
import java.util.*;
public class stringfunction
{
public static void main (String args [])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a complete Sentence = ");
String Sentance = in.nextLine();
String sen [] = Sentance.split(" ");
System.out.print("Numbers of words in your Sentance = " + sen.length + "\n");
try
{
System.out.print("\n Third word from your Sentance = " + sen [2] + "\n");
}
catch (Exception e)
{
System.out.println("Array out of Bound");
}
}
}
Submitted To : Mam Samia Riaz
Submitted By : Danish Ejaz
Registration no : FA21-BSE-074
Section : BSE-3B
Department : Software Engineering

Question no 2:
Write a program having method Merger, which will take String array as an argument. It
combines or merges all the elements of the String array into one string and return that string to
main for display.?
Answer:
import java.util.*;
public class stringfunction
{
public static String marger (String arr [])
{
String arrayString = " ";
for (int i = 0; i < arr.length; i++)
{
arrayString =arrayString+arr[i];
}
return arrayString;
}
public static void main (String args [])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a complete Sentence = ");
String Sentance = in.nextLine ();
String sen [] = Sentance.split(" ");
String returnstring = marger(sen);
System.out.println("Returning String is = " + returnstring);
}
}
Submitted To : Mam Samia Riaz
Submitted By : Danish Ejaz
Registration no : FA21-BSE-074
Section : BSE-3B
Department : Software Engineering

Question no 3:
Write a program having a method that takes a String as an argument and extracts all words
of length 4 to 5 and having vowels in it and place these words in an array of Strings. This method
will return the array of String containing all the words which satisfies the above criteria.?
Answer:
import java.util.Scanner;
public class vowels
{
public static void VOWEL (String SENTENCE)
{
String [] a;
String [] seperatedString = SENTENCE.split(" ");
String arr [] = new String [seperatedString.length];
for (int i =0; i < arr.length; i++)
{
arr[i] = seperatedString[i];
if(arr[i].length() == 4 || arr[i].length() == 5)
{
if(arr[i].contains("a") || arr[i].contains("e") || arr[i].contains("i") ||
arr[i].contains("o") || arr[i].contains("u") || arr[i].contains("A") ||
arr[i].contains("E") || arr[i].contains("I") || arr[i].contains("O") ||
arr[i].contains("U"))
{
System.out.println(arr[i]);
}
}
}
}
public static void main (String args [])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a Complete String = ");
String sentence = in.nextLine();
VOWEL(sentence);
}
}

You might also like