Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views3 pages

String Handling 1

Uploaded by

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

String Handling 1

Uploaded by

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

import java.util.

Scanner;
public class Stringhandling1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n;
do {
System.out.println();
System.out.println("MENU:");
System.out.println("1.Sorting");
System.out.println("2.Comparison");
System.out.println("3.substring()");
System.out.println("4.Concatenation");
System.out.println("5.Length of the string");
System.out.println("6.Replace");
System.out.println("7.Case conversion");
System.out.println("8.Exit");
System.out.println("Enter your choice:");
n = sc.nextInt();
sc.nextLine();
System.out.println();
switch (n) {
case 1:
System.out.println("Enter the number of
things to be sorted:");
int a = sc.nextInt();
sc.nextLine();
String[] things = new String[a];
System.out.println("Enter the things:");
for (int i = 0; i < a; i++) {
things[i] = sc.nextLine();
}

for (int i = 0; i < a - 1; i++) {


for (int j = i + 1; j < a; j++) {
if (things[j].compareTo(things[i])
< 0) {
String t = things[i];
things[i] = things[j];
things[j] = t;
}
}
}
System.out.println("Sorted list:");
for (int i = 0; i < a; i++) {
System.out.println(things[i]);
}
break;
case 2:
System.out.println("Enter the first
string:");
String g1 = sc.nextLine();
System.out.println("Enter second string:");
String g2 = sc.nextLine();
System.out.println("Are strings equal? " +
g1.equals(g2));
break;
case 3:
System.out.println("Enter the string:");
String r1 = sc.nextLine();
System.out.println("Enter the starting
position to be extracted:");
int pos1 = sc.nextInt();
System.out.println("Enter the ending
position to be extracted:");
int pos2 = sc.nextInt();
sc.nextLine();
String r2 = r1.substring(pos1, pos2);
System.out.println("Substring: " + r2);
break;
case 4:
System.out.println("Enter the first
string:");
String s1 = sc.nextLine();
System.out.println("Enter the second
string:");
String s2 = sc.nextLine();
String s3 = s1.concat(s2);
System.out.println("Concatenated string is:
" + s3);
break;
case 5:
System.out.println("Enter the string:");
String b = sc.nextLine();
System.out.println("The length of the
string is: " + b.length());
break;
case 6:
System.out.println("Enter the string:");
String d1 = sc.nextLine();
System.out.println("Enter the character to
be replaced:");
char c1 = sc.nextLine().charAt(0);
System.out.println("Enter the new
character:");
char c2 = sc.nextLine().charAt(0);
String d4 = d1.replace(c1, c2);
System.out.println("Original String: " +
d1);
System.out.println("Replaced String: " +
d4);
break;
case 7:
System.out.println("Enter the string:");
String f1 = sc.nextLine();
System.out.println("Uppercase: " +
f1.toUpperCase());
System.out.println("Lowercase: " +
f1.toLowerCase());
break;
case 8:return;
default:
System.out.println("Invalid choice");
break;
}
} while (n != 8);
}
}

You might also like