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

String Manipulation with Char Arrays

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)
25 views2 pages

String Manipulation with Char Arrays

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

1 package BCA25;

2
3 /*[Link] 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 [Link];
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 >= [Link]) {
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([Link]);
48
49 // Input first string
50 [Link]("Enter the first string: ");
51 String input1 = [Link]();
52 char[] strArray1 = [Link]();
53
54 // Input second string
55 [Link]("Enter the second string: ");
56 String input2 = [Link]();
57 char[] strArray2 = [Link]();
58
59 // a. String length
60 int length1 = getStringLength(strArray1);
61 [Link]("Length of the first string: " + length1);
62
63 // b. Finding a character at a particular position
64 [Link]("Enter the position to find the character in the first string
(0-based index): ");
65 int position = [Link]();
66 try {
67 char charAtPosition = getCharacterAtPosition(strArray1, position);
68 [Link]("Character at position " + position + ": " +
charAtPosition);
69 } catch (IllegalArgumentException e) {
70 [Link]([Link]());
71 }
72
73 // c. Concatenating two strings
74 char[] concatenatedString = concatenateStrings(strArray1, strArray2);
75 [Link]("Concatenated string: " + new String(concatenatedString));
76
77 [Link]();
78 }
79 }
80

Common questions

Powered by AI

The program uses 0-based indexing for accessing the character array. It requires users to input positions based on a 0-based index, which might be less intuitive for some users accustomed to 1-based indexing. Informing users of this in the prompt helps avoid off-by-one errors when retrieving characters from specific positions .

The toCharArray() method is used to convert the user input strings into character arrays, which allows the program to apply string operations specifically designed for handling character arrays, such as calculating string length, finding characters at specific positions, and concatenating arrays .

The program ensures user-friendly error handling by catching the IllegalArgumentException thrown when an invalid character position is requested. It then prints a user-friendly error message indicating that the position is out of range, instead of allowing the program to crash .

To handle strings with special characters, the program can be enhanced to include additional validation checks to correctly process ASCII and Unicode characters. More advanced character handling methods or libraries that support multi-byte characters might be necessary to avoid misinterpretation or truncation of special characters .

While iterating through the character array to calculate the string's length is simple and demonstrates manual handling of arrays, using built-in methods such as string.length() would be more efficient. Built-in methods are often optimized at a lower level for performance, making them faster and less error-prone than manual iteration, especially for large strings .

The getCharacterAtPosition method may throw an IllegalArgumentException if the specified position is out of the valid range of indices for the character array. This occurs when the position is less than 0 or greater than or equal to the array’s length, indicating an invalid index .

Closing the Scanner object is important to release system resources associated with it. Failing to close it could lead to resource leaks, which might degrade system performance or cause memory issues over time since Scanner holds the input stream open .

The concatenateStrings method concatenates two character arrays by first determining the length of each array, then creating a new result array with a size equal to the total of both lengths. It copies elements from the first array into the result array, followed by the elements of the second array. The combined array is then returned as the concatenated result .

The program is limited in scalability as it requires separate custom methods for each string operation, which can become cumbersome when additional operations are necessary. It also lacks adaptability for handling diverse string operations, such as handling strings with different encodings or performing case-insensitive comparisons. Leveraging well-tested libraries for string operations could enhance both scalability and adaptability .

The program finds the length of a string represented as a character array by iterating over the array and incrementing a counter until the end of the array is reached. This counter serves as the length of the string .

You might also like