forked from dscgecbsp/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringoperaations.java
61 lines (53 loc) · 1.91 KB
/
Stringoperaations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Write a JAVA program to Perform string operations like accepting string from user,printing string as output
finding length of string, reverse, concatenation and substring using StringBuilder class.
*/
import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner scn = new Scanner (System.in);
System.out.println ("Enter your string:");
String s1 = scn.nextLine ();
StringBuilder str1 = new StringBuilder ();
str1.append (s1);
System.out.println ("Enter your string:");
String s2 = scn.nextLine ();
StringBuilder str2 = new StringBuilder ();
str2.append (s2);
//choosing an option
System.out.println ("Enter your choice:");
System.out.println ("1-to find length");
System.out.println ("2-to concnetate strings");
System.out.println ("3-to find substring");
System.out.println ("4-to compare strings");
System.out.println ("5-to reverse strings");
int n = scn.nextInt ();
if (n == 1)
{//finding length of string using length() function
System.out.println ("the length of string 1 is:" + str1.length ());
System.out.println ("the length of string 2 is:" + str2.length ());
}
else if (n == 2)
{//Concatenation of strings using '+' operator
System.out.println ("your concanetated string is :" + str1 + " " +
str2);
}
else if (n == 3)
{//finding substring of given string
System.out.println ("Enter number to find substring from :");
int x = scn.nextInt ();
System.out.println ("the substring is:" + str1.substring (x));
}
else if (n == 4)
{//comparing strings
System.out.println
("The result of whether your strings are equal or not is " +
str1.equals (str2));
}
else if(n==5){//reversing the string using reverse() function
System.out.println ("the reversed string is:" + str1.reverse());
}
}
}