0% found this document useful (0 votes)
44 views26 pages

Java String Manipulation Lab Activity

The document contains code for a Java program with three parts: 1) Read inputs using a scanner to form a two dimensional string jagged array. 2) Implement ten string inbuilt functions like length(), toUpperCase(), equals() etc to make the code a real time string application. 3) Implement five of the string functions from part 2 to test the real time application.

Uploaded by

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

Java String Manipulation Lab Activity

The document contains code for a Java program with three parts: 1) Read inputs using a scanner to form a two dimensional string jagged array. 2) Implement ten string inbuilt functions like length(), toUpperCase(), equals() etc to make the code a real time string application. 3) Implement five of the string functions from part 2 to test the real time application.

Uploaded by

Shalni Pandey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LAB ACTIVITY 1

NAME: SHALNI PANDEY COURSE CODE: CSE1007

REG NO: 19BCE2631 SLOT: A2/TA2

Write a java program for the following operations.

(i)     Read the inputs using command line arguments/scanner class to


form two dimensional String jagged array.

Code:

import [Link].*;

public class Question1{

public static void main(String [] args){

Scanner input = new Scanner([Link]);

[Link]("Enter number of arrays: ");

int arrays = [Link]();

String[][] array = new String[arrays][];

for (int i = 0; i < arrays; i++) {

[Link]("Enter number of elements for array #" + i + ": ");

int elements = [Link]();

[Link]();
array[i] = new String[elements];

for (int j = 0; j < elements; j++) {

[Link]("Enter string: ");

String word = [Link]();

array[i][j] = word;

int[] indices = new int[[Link]];

while (indices[0] < array[0].length) {

StringBuilder sb = new StringBuilder();

for (int i = 0; i < [Link]; ++i) {

if (i > 0) {

[Link](' ');

[Link](array[i][indices[i]]);

[Link]([Link]());

for (int i = [Link] - 1; i >= 0; --i) {

if (++indices[i] < array[i].length) {

break;
}

if (i != 0) {

indices[i] = 0;

Output:
(ii)    Use any ten String inbuilt functions to make your java code into meaningful real time string
application.

Code:

import [Link];

public class Question2

public static void main(String args[])

String str;

int len;

Scanner scan = new Scanner([Link]);

[Link]("Enter a String : ");

str = [Link]();

[Link]();

[Link]("String inbuilt functions ");

[Link]("----------------------------");

[Link]("Press : ");

[Link]("--------");

[Link]("1. To find the length of the string.");

[Link]("2. To convert the string to upper case letters.");

[Link]("3. To convert the string to lower case letters.");


[Link]("4. To return the character at a specified index.");

[Link]("5. To concatenate with other strings.");

[Link]("6. To find out if a string contains a sequence of characters.");

[Link]("7. To compare with strings.");

[Link]("8. To compare strings to find out if they are equal.");

[Link]("9. To search a string for the first occurrence of a character.");

[Link]("[Link] find the reverse of the string.");

[Link]();

[Link]("Enter the Choice : ");

int choice = [Link]();

[Link]();

switch (choice) {

case 1:

len = [Link]();

[Link]("Length of Entered String : " + len);

break;

case 2:

[Link]("String in upper case : " + [Link]());

break;

case 3:

[Link]("String in lower case : " + [Link]());

break;

case 4:
[Link]("Enter the index : ");

int i = [Link]();

[Link]("The character at index " + i + " is " + [Link](i));

break;

case 5:

Scanner scan1 = new Scanner([Link]);

[Link]("Enter the string to be concatenated : ");

String str1 = [Link]();

[Link]("The string after concatenation : " + [Link](str1));

break;

case 6:

Scanner scan2 = new Scanner([Link]);

[Link]("Enter the sequence of characters to be found out : ");

String str2 = [Link]();

[Link]("Result : " + [Link](str2));

break;

case 7:

Scanner scan3 = new Scanner([Link]);

[Link]("Enter the String to be compared : ");

String str3 = [Link]();

if([Link](str3) > 0)

[Link]("First String is Greater than Second String.");

else if([Link](str3) < 0)


[Link]("First String is Smaller than Second String.");

else

[Link]("Both Strings are Equal.");

break;

case 8:

Scanner scan4 = new Scanner([Link]);

[Link]("Enter the String to be compared : ");

String str4 = [Link]();

[Link]("Result : " + [Link](str4));

break;

case 9:

Scanner scan5 = new Scanner([Link]);

[Link]("Enter the String or character : ");

String str5 = [Link]();

[Link]("The first occurence of " + str5 + " is at index -> " + [Link](str5));

break;

case 10:

String reverse = new StringBuffer(str).reverse().toString();

[Link](reverse);

break;

}
Output:
(iii)   Implement any five String inbuilt functions which you used in part-ii, to test the real time application.

Code:

import [Link];

public class Question3

{
public static void main(String args[])

String str;

int len;

Scanner scan = new Scanner([Link]);

[Link]("Enter a String : ");

str = [Link]();

[Link]();

[Link]("String inbuilt functions ");

[Link]("----------------------------");

[Link]("Press : ");

[Link]("--------");

[Link]("1. To find the length of the string.");

[Link]("2. To convert the string to upper case letters.");

[Link]("3. To convert the string to lower case letters.");

[Link]("4. To concatenate with other strings.");

[Link]("5. To find the reverse of the string.");

[Link]();

[Link]("Enter the Choice : ");

int choice = [Link]();

[Link]();

switch (choice) {
case 1:

char[] leng= [Link]();

int a=0;

for(char ch : leng)

a++;

[Link]("Length of a string is :"+a);

break;

case 2:

int temp;

String res=" ";

for(int i=0;i<[Link]();i++){

char ch=[Link](i);

temp = (int) ch;

if(temp>=97 && temp<=122){

temp = temp - 32;

res =res+ (char) temp;

else{

res=res+ch;

}
[Link]("Equivalent string in Uppercase = " +res);

break;

case 3:

int tempo;

String resu=" ";

for(int i=0;i<[Link]();i++){

char ch=[Link](i);

tempo = (int) ch;

if(tempo>=65 && tempo<=90){

tempo = tempo + 32;

resu =resu+ (char) tempo;

else{

resu=resu+ch;

[Link]("Equivalent string in Lowercase = " +resu);

break;

case 4:

Scanner scan1 = new Scanner([Link]);

[Link]("Enter the string to be concatenated : ");

String str1 = [Link]();

for(int i=0;i<[Link]();i++){
str=str+[Link](i);

[Link]("The string after concatenation : " + str);

break;

case 5:

String reverse ="";

for(int i=[Link]()-1;i>=0;i--){

reverse=reverse+[Link](i);

[Link](reverse);

break;

Output:

Common questions

Powered by AI

The iterative mechanism in the Java program employs nested loops and index arrays to traverse and process the jagged array. The outer loop iteratively updates an index tracker for each sub-array, resetting indices as appropriate, while the inner logic constructs and prints strings from each sub-array's current element. This method, while efficient for processing lists of varying lengths, requires careful index management to prevent errors such as ArrayIndexOutOfBoundsException, ensuring each iteration accounts for varying sub-array lengths .

The jagged array structure in the Java program allows each sub-array to have a different number of elements, which provides flexibility in data handling. Instead of a regular multi-dimensional array where all rows must be of the same length, jagged arrays can vary in length, making them ideal for applications where data inputs are irregular or vary in size. This flexibility is leveraged in the program to accept user input for different numbers of elements per array, and then to iterate through these arrays, combining elements from each into single strings, enabling outputs to vary based on input lengths .

In the Java program, command line arguments or the Scanner class are used to obtain user input, which is essential for dynamically constructing the jagged array. The Scanner class facilitates runtime user interaction by allowing users to input data at execution time, thus enabling dynamic construction and processing of the jagged array based on user-provided arrays and their elements. This mechanism supports flexible data structures that adjust to user input, offering real-time input handling .

Reversing a string is a fundamental operation that demonstrates essential concepts in string manipulation such as iteration, character indexing, and dynamic string construction. Implementing this operation helps learners grasp how strings can be manipulated by accessing and reordering individual characters, expanding their understanding of string data structures and control flow in programming. This insight is foundational for more complex manipulations often required in real-world software development tasks .

The Java program demonstrates string manipulations by implementing inbuilt functions that handle various common string operations, such as finding the length of a string, converting to uppercase or lowercase, discovering a character at a specific index, concatenation, checking for substring presence, and reversing strings. These operations are facilitated by the String class and functions such as length(), toUpperCase(), toLowerCase(), charAt(), concat(), contains(), and more. This approach showcases practical string handling techniques that are essential in text processing and manipulation tasks .

Using manual ASCII operations for string manipulations demands a meticulous understanding of character encoding, the ASCII table, and potential edge cases such as non-alphabetic characters. It increases code verbosity and the potential for error when compared to Java's inbuilt methods like toUpperCase() or toLowerCase(), which are both concise and optimized for performance. In choosing between these approaches, developers must weigh the need for low-level control against the cost of increased complexity and maintenance overhead .

The choice of inbuilt functions significantly impacts the development of a real-time string application by determining the efficiency, reliability, and scope of functional capabilities. Functions such as length(), toUpperCase(), toLowerCase(), charAt(), concat(), and contains() can streamline tasks like length determination, case conversion, character retrieval, concatenation, and substring checking, essential for dynamic text handling. Effective selection and integration of these functions result in applications that are robust, user-friendly, and performant, capable of addressing diverse text processing requirements in real-time scenarios .

The manual conversion of string case in the Java program involves iterating through the characters of the string, altering ASCII values to switch between uppercase and lowercase, which results in a labor-intensive and error-prone process compared to using built-in methods like toUpperCase() and toLowerCase(). While manual handling allows deeper understanding and customization, built-in methods provide benefits such as optimized performance, reliability, and reduced coding complexity, enhancing maintainability and reducing the potential for errors in case conversion tasks .

The use of multiple Scanner instances for handling user inputs in the Java program allows for distinct input contexts, enabling focused interaction for each string operation. However, it also introduces potential for resource inefficiency and the risk of input stream conflicts. Alternate approaches like using a single scanner instance can streamline input handling, reducing overhead while avoiding the complexities of managing multiple input streams. Despite these drawbacks, multiple scanners can enhance the user experience by allowing differentiated structuring of input phases .

The switch-case statement provides a clear and concise way to handle multiple string operations by directing the program to execute specific blocks of code based on user input, allowing structured and readable program flow. This method reduces the complexity and potential errors associated with using multiple if-else statements, making the code easier to maintain and extend. Each case within the switch handles a distinct string operation, facilitating modular design and enabling easy updates or additions of operations .

You might also like