Save 13
Save 13
import java.util.Scanner;
import java.util.Arrays;
do {
displayMenu();
System.out.print("\t\tEnter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice)
{
case 1:
getStringLength(scanner);
break;
case 2:
findCharPosition(scanner);
break;
case 3:
concatenateStrings(scanner);
break;
case 4:
removeSpaces(scanner);
break;
case 5:
reverseCharArray(scanner);
break;
case 6:
isStringEmpty(scanner);
break;
case 7:
System.out.println("\n\t\tExiting program.");
break;
default:
System.out.println("\n\t\tInvalid choice. Please try again.");
24AI104
Hariharan.R
}
System.out.println(); // Empty line for readability
} while (choice != 7);
scanner.close();
}
System.out.println(title);
System.out.println("\t\t" + stars);
System.out.println("\t\t1. String Length");
System.out.println("\t\t2. Find Character at Position");
System.out.println("\t\t3. Concatenate Two Strings");
System.out.println("\t\t4. Remove Spaces");
System.out.println("\t\t5. Reverse Character Array");
System.out.println("\t\t6. Check if String is Empty");
System.out.println("\t\t7. Exit\n");
}
24AI104
Hariharan.R
System.out.print("\t\tEnter the position (1-indexed) to find character: ");
int position = scanner.nextInt();
scanner.nextLine(); // Consume newline
if (charArray.length == 0)
{
System.out.println("\t\tThe string is empty.");
}
else
24AI104
Hariharan.R
{
System.out.println("\t\tThe string is not empty.");
}
}
}
Output:
String Manipulation Using Character Array
***********************************
1. String Length
2. Find Character at Position
3. Concatenate Two Strings
4. Remove Spaces
5. Reverse Character Array
6. Check if String is Empty
7. Exit
24AI104
Hariharan.R
Enter the first string: H E Y H I
Enter the second string: hello
Concatenated String: H E Y H I hello
Exiting program.
24AI104
Hariharan.R
Matrix Multiplication
import java.util.Scanner;
// Initialize matrices
int[][] firstMatrix = new int[firstMatrixRows][firstMatrixCols];
int[][] secondMatrix = new int[secondMatrixRows][secondMatrixCols];
System.out.println();
scanner.close();
}
}
24AI104
Hariharan.R
Output:
Matrix multiplication
*********************
24AI104
Hariharan.R
Performing string operations using Stringbuffer class
import java.util.Scanner;
while (true)
{
// Display menu
System.out.println("\t\tChoose an option:");
System.out.println("\t\t1. Find string length");
System.out.println("\t\t2. Reverse string");
System.out.println("\t\t3. Delete part of string");
System.out.println("\t\t4. Add to string");
System.out.println("\t\t5. Insert into string");
System.out.println("\t\t6. Replace part of string");
System.out.println("\t\t7. Exit");
if (choice == 7)
{
System.out.println("\n\t\tGoodbye!\n");
break;
}
switch (choice)
{
case 1:
findLength(scanner);
break;
case 2:
reverseString(scanner);
break;
case 3:
deletePart(scanner);
break;
case 4:
addString(scanner);
break;
24AI104
Hariharan.R
case 5:
insertString(scanner);
break;
case 6:
replacePart(scanner);
break;
default:
System.out.println("\n\t\tWrong choice, try again.\n");
}
}
scanner.close();
}
24AI104
Hariharan.R
Output:
STRINGBUFFER OPERATIONS
***********************
Choose an option:
1. Find string length
2. Reverse string
3. Delete part of string
4. Add to string
5. Insert into string
6. Replace part of string
7. Exit
Your choice: 1
Enter a string: Hello world
Length: 11
Choose an option:
1. Find string length
2. Reverse string
3. Delete part of string
4. Add to string
5. Insert into string
6. Replace part of string
7. Exit
Your choice: 2
Enter a string: Artificial intelligence
Reversed: ecnegilletni laicifitrA
Choose an option:
1. Find string length
2. Reverse string
3. Delete part of string
4. Add to string
5. Insert into string
6. Replace part of string
7. Exit
Your choice: 3
Enter a string: Hello world
Start position (1-based): 2
Number of characters to delete: 4
After delete: H world
Choose an option:
1. Find string length
2. Reverse string
3. Delete part of string
4. Add to string
5. Insert into string
24AI104
Hariharan.R
6. Replace part of string
7. Exit
Your choice: 4
Enter first string: hello world
Enter string to add: python
After adding: hello worldpython
Choose an option:
1. Find string length
2. Reverse string
3. Delete part of string
4. Add to string
5. Insert into string
6. Replace part of string
7. Exit
Your choice: 5
Enter a string: Hello World
Enter string to insert: Hi
Insert position (1-based): 8
After insert: Hello WHiorld
Choose an option:
1. Find string length
2. Reverse string
3. Delete part of string
4. Add to string
5. Insert into string
6. Replace part of string
7. Exit
Your choice: 6
Enter a string: Hello world
Start position (1-based): 2
End position (1-based): 6
Enter replacement: K
After replace: HKworld
Choose an option:
1. Find string length
2. Reverse string
3. Delete part of string
4. Add to string
5. Insert into string
6. Replace part of string
7. Exit
Your choice: 7
Goodbye!
24AI104
Hariharan.R
Performing string operations using String class
import java.util.Scanner;
import java.util.Arrays;
while (true)
{
// Display menu
System.out.println("\t\tChoose an option:");
System.out.println("\t\t1. Join strings");
System.out.println("\t\t2. Find substring");
System.out.println("\t\t3. Get substring");
System.out.println("\t\t4. Change character");
System.out.println("\t\t5. String to char array");
System.out.println("\t\t6. Compare strings (ignore case)");
System.out.println("\t\t7. Exit");
if (choice == 7)
{
System.out.println("\n\t\tGoodbye!\n");
break;
}
switch (choice)
{
case 1:
joinStrings(scanner);
break;
case 2:
findSubstring(scanner);
break;
case 3:
getSubstring(scanner);
break;
case 4:
24AI104
Hariharan.R
changeChar(scanner);
break;
case 5:
toCharArray(scanner);
break;
case 6:
compareIgnoreCase(scanner);
break;
default:
System.out.println("\n\t\tWrong choice, try again.\n");
}
}
scanner.close();
}
24AI104
Hariharan.R
Output:
STRING OPERATIONS
*****************
Choose an option:
1. Join strings
2. Find substring
3. Get substring
4. Change character
5. String to char array
6. Compare strings (ignore case)
7. Exit
Your choice: 1
Enter first string: Hello
Enter second string: World
Joined: HelloWorld
Choose an option:
1. Join strings
2. Find substring
3. Get substring
4. Change character
5. String to char array
6. Compare strings (ignore case)
7. Exit
Your choice: 2
Enter main string: Hello
Enter substring to find: ell
Found it!
Choose an option:
1. Join strings
2. Find substring
3. Get substring
4. Change character
5. String to char array
6. Compare strings (ignore case)
7. Exit
Your choice: 3
Enter string: Hello world
Start position (1-based): 2
End position (1-based): 6
Substring: ello
Choose an option:
1. Join strings
2. Find substring
3. Get substring
24AI104
Hariharan.R
4. Change character
5. String to char array
6. Compare strings (ignore case)
7. Exit
Your choice: 4
Enter string: Hello World
Old character: l
New character: N
Changed: HeNNo WorNd
Choose an option:
1. Join strings
2. Find substring
3. Get substring
4. Change character
5. String to char array
6. Compare strings (ignore case)
7. Exit
Your choice: 5
Enter string: Hello World
Char array: [H, e, l, l, o, , W, o, r, l, d]
Choose an option:
1. Join strings
2. Find substring
3. Get substring
4. Change character
5. String to char array
6. Compare strings (ignore case)
7. Exit
Your choice: 6
Enter first string: Hi how are you
Enter second string: HI HOW ARE YOU
Same (ignoring case).
Choose an option:
1. Join strings
2. Find substring
3. Get substring
4. Change character
5. String to char array
6. Compare strings (ignore case)
7. Exit
Your choice: 7
Goodbye!
24AI104
Hariharan.R
Text Analyzer
import java.util.Scanner;
if (text.length() == 0)
{
System.out.println("\t\tNo text entered.");
scanner.close();
return;
}
System.out.println("\n\t\tANALYSIS RESULTS");
System.out.println("\t\t****************");
System.out.println("\t\tCharacters:\t\t" + chars);
System.out.println("\t\tLines:\t\t\t" + lines);
System.out.println("\t\tWords:\t\t\t" + words);
System.out.println("\t\tVowels:\t\t\t" + vowels);
System.out.println("\t\tConsonants:\t\t" + consonants);
24AI104
Hariharan.R
System.out.println("\t\tNumbers:\t\t" + numbers);
System.out.println("\t\tSpaces:\t\t\t" + spaces);
System.out.println("\t\tNewlines:\t\t" + newlines);
System.out.println("\t\tSpecial Characters:\t" + special);
scanner.close();
}
}
Output:
Hi How are you ! Iam fine How about you ?? Can you Drop me at 2:00 P.M at Google Office
@Day.
ANALYSIS RESULTS
*******************
Characters: 94
Lines: 1
Words: 22
Vowels: 30
Consonants: 32
Numbers: 3
Spaces: 21
Newlines: 0
Special Characters: 8
Hey Iam Studying Artificial intelligence and Data Science at Anna UNiversity.
And be proud to myself.
My number is 425267671.
email is hhari@#$.com
Bye!!........
24AI104
Hariharan.R
ANALYSIS RESULTS
*******************
Characters: 164
Lines: 5
Words: 24
Vowels: 45
Consonants: 67
Numbers: 9
Spaces: 21
Newlines: 4
Special Characters: 18
24AI104
Hariharan.R