String Problems
Reverse a String
import java.util.Scanner;public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string to reverse: ");
String input = scanner.nextLine();
String reversed = new StringBuilder(input).reverse().toString();
System.out.println("Reversed string: " + reversed);
scanner.close();
}
}
String reversed = new StringBuilder(input).reverse().toString();
System.out.println("Reversed string: " + reversed);
scanner.close();
}
}
without using StringBuilder
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string to reverse: ");
String input = scanner.nextLine();
// Manually reverse the string
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
System.out.println("Reversed string: " + reversed);
scanner.close();
}
}
************************************************************
Palindrome checking
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string to check if it is a palindrome: ");
String input = scanner.nextLine();
if (isPalindrome(input)) {
System.out.println("The string \"" + input + "\" is a palindrome.");
} else {
System.out.println("The string \"" + input + "\" is not a palindrome.");
}
scanner.close();
}
public static boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
palindrome checking after reversing the string
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string to check if it is a palindrome: ");
String input = scanner.nextLine();
String reversed = reverseString(input);
if (input.equals(reversed)) {
System.out.println("The string \"" + input + "\" is a palindrome.");
} else {
System.out.println("The string \"" + input + "\" is not a palindrome.");
}
scanner.close();
}
public static String reverseString(String s) {
String reversed = "";
for (int i = s.length() - 1; i >= 0; i--) {
reversed += s.charAt(i);
}
return reversed;
}
}
**********************************************************************
Frequency of occurrence of a character in the string
import java.util.Scanner;
public class CharacterFrequencyCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
System.out.print("Enter the character to count: ");
char characterToCount = scanner.next().charAt(0);
int frequency = countCharacterFrequency(input, characterToCount);
System.out.println("The character '" + characterToCount + "' appears " + frequency + " times in the string.");
scanner.close();
}
public static int countCharacterFrequency(String s, char c) {
int frequency = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
frequency++;
}
}
return frequency;
}
}
**********************************************
Analyse a String
import java.util.Scanner;
public class StringAnalyzer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
int length = input.length();
int digitCount = 0;
int lowercaseCount = 0;
int uppercaseCount = 0;
int specialCharCount = 0;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isDigit(c)) {
digitCount++;
} else if (Character.isLowerCase(c)) {
lowercaseCount++;
} else if (Character.isUpperCase(c)) {
uppercaseCount++;
} else {
specialCharCount++;
}
}
System.out.println("Length of the string: " + length);
System.out.println("Number of digits: " + digitCount);
System.out.println("Number of lowercase letters: " + lowercaseCount);
System.out.println("Number of uppercase letters: " + uppercaseCount);
System.out.println("Number of special characters: " + specialCharCount);
scanner.close();
}
}
***************************************************************
Caesar Cipher Encryption
import java.util.Scanner;
public class CaesarCipher {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string to encrypt: ");
String input = scanner.nextLine();
System.out.print("Enter the shift value: ");
int shift = scanner.nextInt();
String encrypted = encrypt(input, shift);
System.out.println("Encrypted string: " + encrypted);
scanner.close();
}
public static String encrypt(String text, int shift) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isLetter(c)) {
char base = Character.isLowerCase(c) ? 'a' : 'A';
char encryptedChar = (char) ((c - base + shift) % 26 + base);
result.append(encryptedChar);
} else {
result.append(c);
}
}
return result.toString();
}
}
**********************************************************************
Printing even length words in a sentence
public class EvenLengthWords {
public static void main(String[] args) {
String sentence = "This is a java language";
String[] words = sentence.split(" ");
for (String word : words) {
if (word.length() % 2 == 0) {
System.out.print(word + " ");
}
}
}
}
*************************************************************************
Sorting names in alphabetical order
import java.util.Scanner;
class SortNames {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of names: ");
int n = scanner.nextInt();
String names[] = new String[n];
System.out.println("Enter the names:");
for (int i = 0; i < n; i++) {
names[i] = scanner.next();
}
String temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (names[i].compareTo(names[j]) > 0) {
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.println("The names in alphabetical order are:");
for (int i = 0; i < n; i++) {
System.out.println(names[i]);
}
}
}
using built in sort method
import java.util.Scanner;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of names: ");
int n = scanner.nextInt();
String names[] = new String[n];
System.out.println("Enter the names:");
for (int i = 0; i < n; i++) {
names[i] = scanner.next();
}
Arrays.sort(names);
System.out.println("The names in alphabetical order are:");
for (int i = 0; i < n; i++) {
System.out.println(names[i]);
}
}
}
Comments
Post a Comment