String -(Programs 1-10)
100 Java String Programming Questions
1. Write a program to find the length of a string.
class StringLength {
public static void main(String[] args) {
String str = "Hello World";
System.out.println("Length of string: " + str.length());
}
}
2. Write a program to print each character of a string.
public class PrintCharacters {
public static void main(String[] args) {
String str = "Hello Java";
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
}
}
}
3. Write a program to reverse a string.
public class ReverseString {
public static void main(String[] args) {
String str = "Hello";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println("Reversed: " + reversed);
}
}
4. Write a program to check a string is palindrome .
class PalindromeCheck {
public static void main(String[] args) {
String str="Mal ay=alam";
String s = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
ch = (char)(ch + 32); // convert uppercase to lowercase
}
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
s += ch;
}
}
boolean isPalindrome = true;
int left = 0, right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
isPalindrome = false;
break;
}
left++;
right--;
}
if (isPalindrome) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
}
}
5. Count the number of Vowels in a String.
class VowelCount {
public static void main(String[] args) {
String str="maluyilom";
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
ch = (char)(ch + 32);
}
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
System.out.println("Number of vowels in "+str+" - " + count);
}
}
6. Count the number of Consonants in a String.
class ConsonantCount {
public static void main(String[] args) {
String str="abcdefghi";
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
ch = (char)(ch + 32);
}
if (ch >= 'a' && ch <= 'z') {
if (!(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')) {
count++;
}
}
}
System.out.println("Number of consonants: " + count);
}
}
/*
class ConsonantCount {
public static void main(String[] args) {
String str = "abcdefghi".toLowerCase();
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z' && "aeiou".indexOf(ch) == -1) {
count++;
}
}
System.out.println("Number of consonants: " + count);
}
}*/
7. Count the number of Digits in a String.
class CountDigits {
public static void main(String[] args) {
String str = "Hello123World456";
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
count++;
}
}
System.out.println("Digits: " + count);
}
}
8. Count the number of Spaces in a String.
class CountSpaces {
public static void main(String[] args) {
String str = "Hello World Java Program";
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ') {
count++;
}
}
System.out.println("Spaces: " + count);
}
}
9. Count the number of Special Characters in a String.
class CountSpecialChars {
public static void main(String[] args) {
String str = "Hello@Java#2025!";
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (!(ch >= 'a' && ch <= 'z') &&
!(ch >= 'A' && ch <= 'Z') &&
!(ch >= '0' && ch <= '9') &&
ch != ' ') {
count++;
}
}
System.out.println("Special Characters: " + count);
}
}
10. Convert the string into Upper Case.
public class ToUpperCase {
public static void main(String[] args) {
String str = "first hello world java";
String upper = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z') {
upper += (char)(ch - 32);
} else {
upper += ch;
}
}
System.out.println("Uppercase: " + upper);
String str1 = "second hello world java";
String upper1=str1.toUpperCase();
System.out.println("Uppercase: " + upper1);
}
}
Comments
Post a Comment