0% found this document useful (0 votes)
99 views17 pages

Oops Lab5

The document contains 13 questions related to string operations in Java. Each question provides sample code to perform a specific string operation, such as converting a string to a character array, appending strings, finding string length, checking for substrings, sorting strings, generating passwords from names and numbers, rearranging characters in alphabetical order, extracting substrings, deleting characters, and checking for palindromes. The code examples demonstrate various string and string builder/buffer methods in Java like toCharArray(), concat(), trim(), indexOf(), split(), sort(), insert(), append(), replace(), delete(), and toString().

Uploaded by

subham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views17 pages

Oops Lab5

The document contains 13 questions related to string operations in Java. Each question provides sample code to perform a specific string operation, such as converting a string to a character array, appending strings, finding string length, checking for substrings, sorting strings, generating passwords from names and numbers, rearranging characters in alphabetical order, extracting substrings, deleting characters, and checking for palindromes. The code examples demonstrate various string and string builder/buffer methods in Java like toCharArray(), concat(), trim(), indexOf(), split(), sort(), insert(), append(), replace(), delete(), and toString().

Uploaded by

subham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

QUESTION – 1:-

Take a sting from keyboard and convert into character array (new one).

CODE:-
import java.util.*;

public class Stringchar {

public static void main(String args[])

Scanner sc = new Scanner(System.in);

String str = sc.nextLine();

char[] ch = str.toCharArray();

for (char c : ch) {

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;

GeneratePassword(String f,String m,String l,String r) {


this.fname=f;
this.mname=m;
this.lname=l;
this.rollno=r;
generatepassword();
}

GeneratePassword(String f,String l,String r){


this.fname=f;
this.lname=l;
this.rollno=r;
generatepassword();
}
void generatepassword() {
String no;
if(mname==null) {
String f=Character.toString(fname.charAt(0));
String l=Character.toString(lname.charAt(0));
no = Character.toString(rollno.charAt(rollno.length()-1))
+Character.toString(rollno.charAt(rollno.length()-2))
+Character.toString(rollno.charAt(rollno.length()-
3))+Character.toString(rollno.charAt(rollno.length()-4));
no = new StringBuffer(no).reverse().toString();
String result=f + l + no;
System.out.println("the password is: " + result);
}
else {
String f=Character.toString(fname.charAt(0));
String l=Character.toString(lname.charAt(0));
String m=Character.toString(mname.charAt(0));
no= Character.toString(rollno.charAt(rollno.length()-1))
+Character.toString(rollno.charAt(rollno.length()-2))
+Character.toString(rollno.charAt(rollno.length()-3))
+Character.toString(rollno.charAt(rollno.length()-4));
no = new StringBuffer(no).reverse().toString();
String result2=f + m + l + no;
System.out.println("the password is: " + result2);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("does your name has middle name");
String response=sc.next();
if(response.equals("yes")) {
System.out.println("enter first name");
String f=sc.next();
System.out.println("enter middle name");
String m=sc.next();
System.out.println("enter last name");
String l=sc.next();
System.out.println("enter roll no");
String r=sc.next();
GeneratePassword g1=new GeneratePassword(f,m,l,r);
}
else {
System.out.println("enter first name");
String f=sc.next();
System.out.println("enter last name");
String l=sc.next();
System.out.println("enter roll no");
String r=sc.next();
GeneratePassword g1=new GeneratePassword(f,l,r);
}
}
}

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:-

10. Write a program to do the following


i) To output the question “Who is the inventor of Java”?
ii) To accept an answer.
iii) To print out “Good” and then stop, if the answer is correct.
iv) To output the message “Try Again” and then stop, if the answer is wrong.
v) To display the correct answer when the answer is wrong even at the third
attempts and stop.
CODE:-
import java.util.*;
public class question
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String ans;
int attempt=1;
while(attempt<=3)
{
System.out.println("Who is the inventor of Java?");
ans=sc.nextLine();
if(ans.equalsIgnoreCase("James Gosling"))
System.out.println("Good");
else{
System.out.println("Try Again");
attempt++;
}
}
if(attempt>3)
System.out.println("Correct Answer: James Gosling");
}
}
OUTPUT:-
QUESTION – 11:-

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:-

Write a program in Java to create a StringBuffer object with content “Object


Programming languages”. Then perform the following operations
i) Print the object and determine its length and capacity.
ii) Inset a new string “Oriented” after the word “Object”. Print the modified
string.
iii) Insert a character ‘-’ at the 6-th position. Print the modified string again.
iv) Append another string named “ are very popular.” Print the resulting string.
v) Now delete the character ‘-’ and put a space (“ ”) there. Print it.
vi) Apply these operations one-by-one (a) delete(1, 7), (b) delete(2, 10), and
(c) delete(3, 13). Then print the result.
vii) Convert the StringBuffer type object into a String object. Print the final
result.
CODE:-
public class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Object Programming languages\n");
System.out.println("\ncapacity: " + sb.capacity());
System.out.println("length = " + sb.length());
System.out.println(sb);
char cArr[] = { 'O', 'r', 'i', 'e', 'n', 't', 'e', 'd',' ' };
sb.insert(7, cArr);
System.out.println(sb.toString());
sb.insert(5,'-');
System.out.println(sb.toString());
sb.append("are very popular.");
System.out.println(sb);
sb.replace(5, 6, " ");
System.out.println(sb);
sb.delete(1,7);
System.out.println(sb);
sb.delete(2,10);
System.out.println(sb);
sb.delete(3,13);
System.out.println(sb);
System.out.println(sb.toString());
} }
OUTPUT:-
QUESTION – 13:-

Write a program in Java that checks whether a given string is a palindrome or


not. Ignore the cases.
CODE:-
import java.util.Scanner;
public class Palindrome
{
public static void main(String args[])
{
String a, b = "";
Scanner s = new Scanner(System.in);
System.out.print("Enter the string you want to check:");
a = s.nextLine();
int n = a.length();
for(int i = n - 1; i >= 0; i--)
{
b = b + a.charAt(i);
}
if(a.equalsIgnoreCase(b))
{
System.out.println("The string is palindrome.");
}
else
{
System.out.println("The string is not palindrome.");
}
}
}

OUTPUT:-

You might also like