-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringManipulation.java
More file actions
39 lines (27 loc) · 1.29 KB
/
StringManipulation.java
File metadata and controls
39 lines (27 loc) · 1.29 KB
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
import java.util.Scanner;
public class StringManipulation {
public static void main(String[] args) {
try(Scanner scan = new Scanner(System.in)){
System.out.println("Enter the first String: ");
String firstString = scan.nextLine();
char[] firstArray = firstString.toCharArray();
int len = firstArray.length;
System.out.println("The length of the string is " + len);
System.out.println("Enter a position to find a character (0 to " + (len-1) + "): ");
int position = scan.nextInt();
if (position>=0 && position<len) {
char chatAtPosition = firstArray[position];
System.out.println("The character at the position " + position + " is " + chatAtPosition);
}
else{System.out.println("invalid position");}
scan.nextLine(); //clear the buffer
System.out.println("Enter the second String: ");
String secondString = scan.nextLine();
String concatenated = firstString + " " + secondString;
System.out.println("The concatenated string is \n" + concatenated);
}
catch(Exception e){
System.out.println("Please enter String as Input!");
}
}
}