mirror of
https://github.com/Manoj-HV30/OOPS-lab-codes.git
synced 2026-05-16 19:35:25 +00:00
122 lines
3.8 KiB
Java
122 lines
3.8 KiB
Java
import java.util.Scanner;
|
|
|
|
// -------- Helper Class --------
|
|
class TextHelper {
|
|
|
|
private String text;
|
|
private String keyword;
|
|
|
|
TextHelper(String text, String keyword) {
|
|
this.text = text;
|
|
this.keyword = keyword;
|
|
}
|
|
|
|
// 1. View character at a position
|
|
void viewCharacter(int position) {
|
|
if (position >= 0 && position < text.length()) {
|
|
System.out.println(
|
|
"Character at position " +
|
|
position +
|
|
": " +
|
|
text.charAt(position)
|
|
);
|
|
} else {
|
|
System.out.println("Invalid position!");
|
|
}
|
|
}
|
|
|
|
// 2. Compare text and keyword
|
|
void compareText() {
|
|
int result = text.compareTo(keyword);
|
|
|
|
if (result == 0) System.out.println("Text and keyword are equal.");
|
|
else if (result < 0) System.out.println(
|
|
"Text comes before keyword alphabetically."
|
|
);
|
|
else System.out.println("Keyword comes before text alphabetically.");
|
|
}
|
|
|
|
// 3. Search & replace keyword
|
|
void searchAndReplace(String replacement) {
|
|
int index = text.indexOf(keyword);
|
|
|
|
if (index == -1) {
|
|
System.out.println("Keyword not found in text.");
|
|
} else {
|
|
System.out.println("Keyword first found at position: " + index);
|
|
text = text.replace(keyword, replacement);
|
|
System.out.println("Updated Text: " + text);
|
|
}
|
|
}
|
|
|
|
// 4. Extract substring
|
|
void extractPortion(int start, int end) {
|
|
if (start >= 0 && end <= text.length() && start < end) {
|
|
System.out.println("Extracted Text: " + text.substring(start, end));
|
|
} else {
|
|
System.out.println("Invalid start or end position!");
|
|
}
|
|
}
|
|
}
|
|
|
|
// -------- Main Class --------
|
|
public class TextProcessingHelper {
|
|
|
|
public static void main(String[] args) {
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
System.out.print("Enter main text: ");
|
|
String text = sc.nextLine();
|
|
|
|
System.out.print("Enter keyword: ");
|
|
String keyword = sc.nextLine();
|
|
|
|
TextHelper helper = new TextHelper(text, keyword);
|
|
|
|
int choice;
|
|
|
|
do {
|
|
System.out.println("\n--- Text Processing Menu ---");
|
|
System.out.println("1. View character at a position");
|
|
System.out.println("2. Compare text and keyword");
|
|
System.out.println("3. Search and replace keyword");
|
|
System.out.println("4. Extract portion of text");
|
|
System.out.println("5. Exit");
|
|
System.out.print("Enter your choice: ");
|
|
|
|
choice = sc.nextInt();
|
|
sc.nextLine(); // consume newline
|
|
|
|
switch (choice) {
|
|
case 1:
|
|
System.out.print("Enter position: ");
|
|
int pos = sc.nextInt();
|
|
helper.viewCharacter(pos);
|
|
break;
|
|
case 2:
|
|
helper.compareText();
|
|
break;
|
|
case 3:
|
|
System.out.print("Enter replacement word: ");
|
|
String replacement = sc.nextLine();
|
|
helper.searchAndReplace(replacement);
|
|
break;
|
|
case 4:
|
|
System.out.print("Enter start position: ");
|
|
int start = sc.nextInt();
|
|
System.out.print("Enter end position: ");
|
|
int end = sc.nextInt();
|
|
helper.extractPortion(start, end);
|
|
break;
|
|
case 5:
|
|
System.out.println("Exiting program...");
|
|
break;
|
|
default:
|
|
System.out.println("Invalid choice!");
|
|
}
|
|
} while (choice != 5);
|
|
|
|
sc.close();
|
|
}
|
|
}
|