Experiment : 4
AIM : Program on Strings & String buffer
Reverse the string and decide whether it is palindrome or not
Capitalize the first letter of each word
Count the frequency of particular letter and replace that particular letter by #
Count the no. of uppercase, lowercase, blank spaces, digits, special characters from string
OBJECTIVE : To make students understand the significance of String Handling in Java.
DESCRIPTION :
What are strings in Java?
Give the List Of String Constructors.
Give the List of
String Functions
category Wise with
Simple description?
Strings in Java are
immutable. Expain
Limitations of String
Class. StringBuffer
in Java.
List of Constructors and Functions
CONCLUSION: Errors and exception faced during Execution
REFERENCES :
PRACTICAL 4
Reverse the string and decide whether it is palindrome or not
import java.util.Scanner;
public class StringPalindromeChecker {
public static void main(String[] args) {
System.out.println("Prachi Thakare Roll number 100");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
StringBuffer reversedString = new StringBuffer(input);
reversedString.reverse();
boolean isPalindrome = input.equalsIgnoreCase(reversedString.toString());
System.out.println("Reversed String: " + reversedString);
if (isPalindrome) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}
scanner.close();
}
}
Capitalize the first letter of each word
import java.util.Scanner;
public class CapitalizeWords {
public static void main(String[] args) {
System.out.println("Prachi Thakare Roll number 0");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
String capitalizedString = capitalizeWords(input);
System.out.println("Capitalized String: " + capitalizedString);
scanner.close();
}
public static String capitalizeWords(String str) {
String[] words = str.split(" ");
StringBuilder capitalized = new StringBuilder();
for (String word : words) {
if (word.length() > 0) {
capitalized.append(Character.toUpperCase(word.charAt(0)))
.append(word.substring(1)).append(" ");
}
}
return capitalized.toString().trim();
}
}
Count the frequency of particular letter and replace that particular letter by #
import java.util.Scanner;
public class LetterFrequencyReplacer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputString = "Prachi Thakare Roll no 100";
System.out.println("Original String: " + inputString);
System.out.print("Enter the letter to count and replace: ");
char letterToReplace = scanner.next().charAt(0);
int frequency = 0;
StringBuilder modifiedString = new StringBuilder();
for (char c : inputString.toCharArray()) {
if (Character.toLowerCase(c) == Character.toLowerCase(letterToReplace)) {
frequency++;
modifiedString.append('#');
} else {
modifiedString.append(c);
}
}
System.out.println("Modified String: " + modifiedString.toString());
System.out.println("Frequency of '" + letterToReplace + "': " + frequency);
scanner.close();
}
}
Count the no. of uppercase, lowercase, blank spaces, digits, special characters
from string
public class CharacterCounter {
public static void main(String[] args) {
String inputString = "Prachi Thakare Roll no 100";
int upperCaseCount = 0;
int lowerCaseCount = 0;
int spaceCount = 0;
int digitCount = 0;
int specialCharCount = 0;
for (char c : inputString.toCharArray()) {
if (Character.isUpperCase(c)) {
upperCaseCount++;
} else if (Character.isLowerCase(c)) {
lowerCaseCount++;
} else if (Character.isWhitespace(c)) {
spaceCount++;
} else if (Character.isDigit(c)) {
digitCount++;
} else {
specialCharCount++;
}
}
System.out.println("Input String: " + inputString);
System.out.println("Number of Uppercase Letters: " + upperCaseCount);
System.out.println("Number of Lowercase Letters: " + lowerCaseCount);
System.out.println("Number of Blank Spaces: " + spaceCount);
System.out.println("Number of Digits: " + digitCount);
System.out.println("Number of Special Characters: " + specialCharCount);
}
}