0% found this document useful (0 votes)
14 views2 pages

StringManipulation5

Uploaded by

PRIYA
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)
14 views2 pages

StringManipulation5

Uploaded by

PRIYA
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/ 2

1 package BCA25;

2
3 /*5.Write a program to do String Manipulation using Character Array and perform the
following string operations:
4 a. String length
5 b. Finding a character at a particular position
6 c. Concatenating two strings
7 */
8 import java.util.Scanner;
9
10 public class StringManipulation5 {
11
12 // Method to find the length of a string
13 public static int getStringLength(char[] strArray) {
14 int length = 0;
15 for (char c : strArray) {
16 length++;
17 }
18 return length;
19 }
20
21 // Method to find a character at a specific position
22 public static char getCharacterAtPosition(char[] strArray, int position) {
23 if (position < 0 || position >= strArray.length) {
24 throw new IllegalArgumentException("Position out of range.");
25 }
26 return strArray[position];
27 }
28
29 // Method to concatenate two character arrays
30 public static char[] concatenateStrings(char[] strArray1, char[] strArray2) {
31 int length1 = getStringLength(strArray1);
32 int length2 = getStringLength(strArray2);
33 char[] result = new char[length1 + length2];
34
35 // Copy first string
36 for (int i = 0; i < length1; i++) {
37 result[i] = strArray1[i];
38 }
39 // Copy second string
40 for (int i = 0; i < length2; i++) {
41 result[length1 + i] = strArray2[i];
42 }
43 return result;
44 }
45
46 public static void main(String[] args) {
47 Scanner scanner = new Scanner(System.in);
48
49 // Input first string
50 System.out.print("Enter the first string: ");
51 String input1 = scanner.nextLine();
52 char[] strArray1 = input1.toCharArray();
53
54 // Input second string
55 System.out.print("Enter the second string: ");
56 String input2 = scanner.nextLine();
57 char[] strArray2 = input2.toCharArray();
58
59 // a. String length
60 int length1 = getStringLength(strArray1);
61 System.out.println("Length of the first string: " + length1);
62
63 // b. Finding a character at a particular position
64 System.out.print("Enter the position to find the character in the first string
(0-based index): ");
65 int position = scanner.nextInt();
66 try {
67 char charAtPosition = getCharacterAtPosition(strArray1, position);
68 System.out.println("Character at position " + position + ": " +
charAtPosition);
69 } catch (IllegalArgumentException e) {
70 System.out.println(e.getMessage());
71 }
72
73 // c. Concatenating two strings
74 char[] concatenatedString = concatenateStrings(strArray1, strArray2);
75 System.out.println("Concatenated string: " + new String(concatenatedString));
76
77 scanner.close();
78 }
79 }
80

You might also like