0% found this document useful (0 votes)
7 views6 pages

Java String Manipulation Case Studies

The document presents ten case studies demonstrating various Java programming tasks, including palindrome checking, password validation, word frequency counting, name formatting, string compression, anagram checking, account number masking, word reversal, longest word finding, and email validation. Each case study includes a problem statement, Java code implementation, and sample output. Additionally, it features five more case studies focusing on string manipulation techniques such as reversing words, formatting student IDs, masking phone numbers, deleting vowels, and more.

Uploaded by

mahaselvan2005
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)
7 views6 pages

Java String Manipulation Case Studies

The document presents ten case studies demonstrating various Java programming tasks, including palindrome checking, password validation, word frequency counting, name formatting, string compression, anagram checking, account number masking, word reversal, longest word finding, and email validation. Each case study includes a problem statement, Java code implementation, and sample output. Additionally, it features five more case studies focusing on string manipulation techniques such as reversing words, formatting student IDs, masking phone numbers, deleting vowels, and more.

Uploaded by

mahaselvan2005
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

Case Study 1: Palindrome Sentence Checker

Problem Statement: Write a Java program to check if a given sentence is a palindrome, ignoring
spaces and case.
Java Code:
public class PalindromeSentence {
public static void main(String[] args) {
String str = "A man a plan a canal Panama";
String clean = [Link]("[^a-zA-Z]", "").toLowerCase();
String reversed = new StringBuilder(clean).reverse().toString();
if ([Link](reversed)) {
[Link]("The sentence is a Palindrome.");
} else {
[Link]("The sentence is NOT a Palindrome.");
}
}
}

Sample Output:
The sentence is a Palindrome.

Case Study 2: Password Strength Validator


Problem Statement: Validate if a password is strong (≥8 chars, uppercase, lowercase, digit,
special char).
Java Code:
public class PasswordValidator {
public static void main(String[] args) {
String password = "Hello@123";
boolean hasUpper = [Link](".*[A-Z].*");
boolean hasLower = [Link](".*[a-z].*");
boolean hasDigit = [Link](".*[0-9].*");
boolean hasSpecial = [Link](".*[@#$%].*");
if ([Link]() >= 8 && hasUpper && hasLower && hasDigit && hasSpecial) {
[Link]("Password is STRONG.");
} else {
[Link]("Password is WEAK.");
}
}
}

Sample Output:
Password is STRONG.

Case Study 3: Word Frequency Counter


Problem Statement: Count how many times a word occurs in a paragraph, ignoring case.
Java Code:
public class WordFrequency {
public static void main(String[] args) {
String paragraph = "Java is simple. Java is powerful. Java is everywhere.";
String word = "java";
String[] words = [Link]().split("\\W+");
int count = 0;
for (String w : words) {
if ([Link]([Link]())) {
count++;
}
}
[Link]("The word '" + word + "' occurs " + count + " times.");
}
}

Sample Output:
The word 'java' occurs 3 times.

Case Study 4: Student Name Formatter


Problem Statement: Convert names stored as LASTNAME,FIRSTNAME to Firstname Lastname
format.
Java Code:
public class NameFormatter {
public static void main(String[] args) {
String rawName = "RAO,ANITA";
String[] parts = [Link](",");
String last = parts[0].substring(0,1).toUpperCase() + parts[0].substring(1).toLowerCase(
String first = parts[1].substring(0,1).toUpperCase() + parts[1].substring(1).toLowerCase
String formatted = first + " " + last;
[Link]("Formatted Name: " + formatted);
}
}

Sample Output:
Formatted Name: Anita Rao

Case Study 5: String Compression Utility


Problem Statement: Compress repeated characters in a string, e.g., aaabbcccc → a3b2c4.
Java Code:
public class StringCompression {
public static void main(String[] args) {
String str = "aaabbccccdd";
StringBuilder result = new StringBuilder();
int count = 1;
for (int i = 1; i <= [Link](); i++) {
if (i < [Link]() && [Link](i) == [Link](i - 1)) {
count++;
} else {
[Link]([Link](i - 1)).append(count);
count = 1;
}
}
[Link]("Original: " + str);
[Link]("Compressed: " + result);
}
}
Sample Output:
Original: aaabbccccdd
Compressed: a3b2c4d2

Case Study 6: Anagram Checker


Problem Statement: Check whether two strings are anagrams of each other.
Java Code:
import [Link];
public class AnagramCheck {
public static void main(String[] args) {
String s1 = "listen";
String s2 = "silent";
char[] arr1 = [Link]().toCharArray();
char[] arr2 = [Link]().toCharArray();
[Link](arr1);
[Link](arr2);
if ([Link](arr1, arr2)) {
[Link](s1 + " and " + s2 + " are Anagrams");
} else {
[Link](s1 + " and " + s2 + " are NOT Anagrams");
}
}
}

Sample Output:
listen and silent are Anagrams

Case Study 7: Mask Account Number


Problem Statement: Mask all digits except the last 4 in an account number.
Java Code:
public class MaskAccount {
public static void main(String[] args) {
String accNo = "9876543210";
String masked = "******" + [Link]([Link]() - 4);
[Link]("Original: " + accNo);
[Link]("Masked: " + masked);
}
}

Sample Output:
Original: 9876543210
Masked: ******3210

Case Study 8: Reverse Words in Sentence


Problem Statement: Reverse order of words in a sentence (Java is fun → fun is Java).
Java Code:
public class ReverseWords {
public static void main(String[] args) {
String sentence = "Java is fun";
String[] words = [Link](" ");
for (int i = [Link] - 1; i >= 0; i--) {
[Link](words[i] + " ");
}
}
}

Sample Output:
fun is Java

Case Study 9: Longest Word Finder


Problem Statement: Find the longest word in a sentence.
Java Code:
public class LongestWord {
public static void main(String[] args) {
String sentence = "Java makes programming easy";
String[] words = [Link](" ");
String longest = "";
for (String w : words) {
if ([Link]() > [Link]()) {
longest = w;
}
}
[Link]("Longest word: " + longest);
}
}

Sample Output:
Longest word: programming

Case Study 10: Email Validation


Problem Statement: Validate email with conditions: must have @, . after @, and end with .com or
.edu.
Java Code:
public class EmailValidation {
public static void main(String[] args) {
String email = "student123@[Link]";
if ([Link]("@") && [Link]('.') > [Link]('@')
&& ([Link](".com") || [Link](".edu"))) {
[Link](email + " is VALID");
} else {
[Link](email + " is INVALID");
}
}
}

Sample Output:
student123@[Link] is VALID
Case Study 1: Palindrome Check using StringBuffer
Problem Statement: Check whether a string is a palindrome using [Link]().
Java Code:
public class PalindromeStringBuffer {
public static void main(String[] args) {
String str = "Level";
StringBuffer sb = new StringBuffer([Link]());
String reversed = [Link]().toString();
if ([Link]().equals(reversed)) {
[Link](str + " is a Palindrome");
} else {
[Link](str + " is NOT a Palindrome");
}
}
}

Sample Output:
Level is a Palindrome

Case Study 2: Insert Formatting in Student ID


Problem Statement: Insert a dash after year in student ID (2025CSE001 → 2025-CSE001).
Java Code:
public class InsertDemo {
public static void main(String[] args) {
StringBuffer id = new StringBuffer("2025CSE001");
[Link](4, "-");
[Link]("Formatted ID: " + id);
}
}

Sample Output:
Formatted ID: 2025-CSE001

Case Study 3: Replace Part of Phone Number


Problem Statement: Mask middle digits of phone number (9876543210 → 98*****210).
Java Code:
public class ReplaceDemo {
public static void main(String[] args) {
StringBuffer phone = new StringBuffer("9876543210");
[Link](2, 7, "*****");
[Link]("Masked Phone: " + phone);
}
}

Sample Output:
Masked Phone: 98*****210

Case Study 4: Reverse Each Word


Problem Statement: Reverse each word in a sentence using [Link]().
Java Code:
public class ReverseWords {
public static void main(String[] args) {
String sentence = "Java is fun";
String[] words = [Link](" ");
for (String w : words) {
StringBuffer sb = new StringBuffer(w);
[Link]([Link]() + " ");
}
}
}

Sample Output:
avaJ si nuf

Case Study 5: Delete Vowels from String


Problem Statement: Remove all vowels from a given string using deleteCharAt().
Java Code:
public class DeleteVowels {
public static void main(String[] args) {
String str = "Programming in Java";
StringBuffer sb = new StringBuffer(str);
for (int i = 0; i < [Link](); i++) {
char c = [Link]([Link](i));
if ("aeiou".indexOf(c) != -1) {
[Link](i);
i--;
}
}
[Link]("Original: " + str);
[Link]("Without vowels: " + sb);
}
}

Sample Output:
Original: Programming in Java
Without vowels: Prgrmmng n Jv

You might also like