Oops Lab5
Oops Lab5
Take a sting from keyboard and convert into character array (new one).
CODE:-
import java.util.*;
char[] ch = str.toCharArray();
System.out.print(c+",");
OUTPUT:-
QUESTION – 2:-
Take a string from keyboard and a char array (filled up to length 5). Now
append the string to that char array. Show the char array.
CODE:-
import java.util.*;
public class Stringchara {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] a = sc.next().toCharArray();
String str2= String.valueOf(a);
String str3= str+ str2;
char[] ch = str3.toCharArray();
for (char c : ch) {
System.out.print(c+",");
}
}
}
OUTPUT:-
QUESTION – 3:-
Find length of a string taken from keyboard and also find the length of
that string except front and end spaces.
CODE:-
import java.util.*;
public class Stringlength {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(str.length());
String str2 =str.trim();
System.out.println(str2.length());
}
}
OUTPUT:-
QUESTION – 4:-
Check if "Tech" presents in "University of Technology" or not. If yes
return its position.
CODE:-
import java.util.*;
public class Stringpos {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
System.out.println("position= "+str1.indexOf(str2));
}
}OUTPUT:-
QUESTION – 5:-
Write a program to take a sentence and convert it into string arrays and
sort the words using any sorting technique.
CODE:-
import java.util.*;
public class Stringsort {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str;
System.out.println("enter the string");
str=sc.nextLine();
String strArray[] = str.split(" ");
System.out.println("String : " + str);
System.out.print("String array : [ ");
for (int i = 0; i < strArray.length; i++) {
System.out.print(strArray[i] + ", ");
}
System.out.print("]");
System.out.println();
Arrays.sort(strArray);
System.out.println("sorted:");
for (int i = 0; i < strArray.length;i++){
System.out.println(strArray[i]);
}
}
}
OUTPUT:-
QUESTION – 6:-
Generate password from initials of one’s first_name, middle_name,
last_name and with last four digit of your roll_no (if middle name is not
present, it won't come).
CODE:-
import java.lang.*;
import java.io.*;
import java.util.*;
public class GeneratePassword {
String fname;
String mname;
String lname;
String rollno;
OUTPUT:-
QUESTION – 7:-
Write a program in Java which will read a string and rewrite it in the
alphabetical
order. For example, the word STRING should be written as GINRST.
CODE:-
import java.util.Arrays;
import java.util.Scanner;
public class wordsort {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string value: ");
String str = sc.nextLine();
char charArray[] = str.toCharArray();
Arrays.sort(charArray);
System.out.println(new String(charArray));
}
}
OUTPUT:-
QUESTION – 8:-
Write a program in Java to extract a portion of a character string and print
the
extracted string. Assume that m characters are extracted, starting with the
n-th
character. The method signature will be like void extract(String str, int n,
int m).
CODE:-
import java.util.*;
public class Extractpor{
static void extract(String str, int n, int m){
String newstr="";
while(m-- > 0){
newstr=newstr+str.charAt(n++);
}
System.out.println(newstr);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String inp;
int n,m;
inp=sc.next();
n=sc.nextInt();
m=sc.nextInt();
extract(inp,n-1,m);
}}
OUTPUT:-
QUESTION – 9:-
Write your own method is having a signature like String deleteMe(String
str, int m) that returns the input string with the m-th element removed.
CODE:-
import java.util.*;
public class delete
{
static String deleteMe(String str, int m)
{
String newstr="";
int i=0;
while(i<str.length())
{
if(i+1==m)
{
i++;
continue;
}
newstr=newstr+str.charAt(i);
i++;
}
return newstr;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String inp;
int m;
inp=sc.next();
m=sc.nextInt();
System.out.println(deleteMe(inp,m));
}
}
OUTPUT:-
QUESTION – 10:-
Show that the String class type objects are immutable but StringBuffer class
objects
CODE:-
class ImmutableMutable{
public static void main(String args[]){
String s="Object Oriented";
s.concat(" Programming");
System.out.println(s);
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");
System.out.println(sb);
}
}
OUTPUT:-
QUESTION – 12:-
OUTPUT:-