Strings are a crucial part of Java programming, enabling developers to work with text efficiently. This guide covers essential Java string programs, from basic operations to advanced string manipulation. Whether you're a beginner or an experienced developer, you'll find these Java string examples and java string exercises helpful for honing your skills.
In Java, strings are objects that represent sequences of characters. The String class in Java provides numerous methods for manipulating and handling strings.
This program reverses a given string using a loop.
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
System.out.println("Reversed String: " + reversed);
}
}
A palindrome reads the same forwards and backwards. This program checks if a string is a palindrome.
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
if (str.equals(reversed)) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
}
}
This program counts the number of vowels and consonants in a string.
public class CountVowelsConsonants {
public static void main(String[] args) {
String str = "Hello World";
int vowels = 0, consonants = 0;
for (char c : str.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) != -1) {
vowels++;
} else if (c >= 'a' && c <= 'z') {
consonants++;
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
}
}
public class LongestWord {
public static void main(String[] args) {
String sentence = "Java programming is fun";
String[] words = sentence.split(" ");
String longest = "";
for (String word : words) {
if (word.length() > longest.length()) {
longest = word;
}
}
System.out.println("Longest word: " + longest);
}
}
import java.util.LinkedHashSet;
public class RemoveDuplicates {
public static void main(String[] args) {
String str = "programming";
StringBuilder result = new StringBuilder();
LinkedHashSet set = new LinkedHashSet<>();
for (char c : str.toCharArray()) {
if (set.add(c)) {
result.append(c);
}
}
System.out.println("String without duplicates: " + result);
}
}
import java.util.Arrays;
public class SortString {
public static void main(String[] args) {
String str = "java";
char[] chars = str.toCharArray();
Arrays.sort(chars);
System.out.println("Sorted string: " + new String(chars));
}
}
The String class is used to create and manipulate strings. It offers methods for operations like concatenation, comparison, and splitting.
Strings are immutable to ensure security, thread safety, and efficient memory usage. Once created, a string's value cannot be changed.
Java provides methods such as substring(), replace(), toUpperCase(), and trim() for string manipulation.
Beginners can start with programs like reversing a string, checking for palindromes, counting vowels and consonants, and finding substrings.
No, strings should be compared using the equals() method for content comparison. The == operator compares references, not content.
Mastering Java string programs is essential for efficient text processing and solving real-world problems. From basic string manipulation to advanced challenges, practicing these java string examples and java string exercises will improve your coding skills. Keep exploring and practicing to become proficient in handling strings in Java.
Copyrights © 2024 letsupdateskills All rights reserved