String (11 to 20) Programs
100 Java String Programming Questions Series(11 to 20)
11. Write a program to convert string into LowerCase.
class StringToLowerCase {
public static void main(String[] args) {
String str = "ApplE";
char c;;
System.out.println("Input given is - " + str);
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (str.charAt(i) >= 65 && str.charAt(i) <= 90) {
c = (char)(c + 32);
System.out.print(c);
} else
System.out.print(c);
}
//System.out.println(str.toLowerCase());
}
}
12.Write a program to convert uppercase letters to lowercase and vice versa..
public class ToggleCase {
public static void main(String[] args) {
String text = "HeLLo WoRLd JaVa123";
System.out.println("Input text is : " + text);
System.out.print("Toggled: ");
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
ch = (char)(ch + 32);
} else if (ch >= 'a' && ch <= 'z') {
ch = (char)(ch - 32);
}
System.out.print(ch);
}
}
}
13.Write a program to remove vowels from a string.
public class RemoveVowels {
public static void main(String[] args) {
String text = "HeLLo WoRLd JaVa123";
System.out.println("Input text is : " + text);
System.out.print("Vowels Removed : ");
int i=0;
while(i
14.Write a program to remove spaces from a string.
public class RemoveSpaces {
public static void main(String[] args) {
String text = "HeLLo Wo RLd JaVa1 23";
System.out.println("Input text is : " + text);
System.out.print("Spaces Removed : ");
int i=0;
while(i
15.Write a program to remove digits from a string.
public class Removedigits {
public static void main(String[] args) {
String text = "HeLLo2025 WoRLd JaVa1 23";
System.out.println("Input text is : " + text);
System.out.print("Spaces Removed : ");
int i=0;
while(i='0' && ch<='9'){
i++;
}
else{
System.out.print(ch);
i++;
}
}
String text1 = "a1b2c3d4e5";
System.out.println("\nInput text is : "+text1);
String result1 = text1.replaceAll("[0-9]", "");
System.out.println("Result: " + result1);
}
}
16.Write a program to remove special cahracters from a string.
public class RemoveSpecialCharacter {
public static void main(String[] args) {
String text = "HeLLo@2025+WoRLd=JaVa1*$23";
System.out.println("Input text is : " + text);
System.out.print("Spaces Removed : ");
int i=0;
while(i=48 && ch<=57||ch>=65 && ch<=90||ch>=97 && ch<=122){
System.out.print(ch);
i++;
}
else{
i++;
}
}
}
}
17.Write a program to replace spaces with underscores in a string.
public class ReplaceSpaces_{
public static void main(String[] args) {
String text = "Hello World Java Program";
System.out.println("Modified: ");
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (ch == ' ') {
System.out.print('_');
} else {
System.out.print(ch);
}
}
String text1 = "He llo Wor ld J ava Program";
System.out.println("\nOriginal: " + text1);
String result1 = text.replace(' ', '_');
System.out.println("Modified: " + result1);
}
}
18.Write a program to replace a character in a string.
public class ReplaceSpaces_{
public static void main(String[] args) {
String text = "I have a cat";
System.out.println("\nOriginal: " + text);
System.out.println("Modified: ");
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (ch == 'c') {
System.out.print('r');
} else {
System.out.print(ch);
}
}
String text1 = "the sun is hot";
System.out.println("\nOriginal: " + text1);
String result1 = text1.replace('s','r');
System.out.println("Modified: " + result1);
}
}
19.Write a program to check strings are equal.
class EqualsCheck {
public static void main(String[] args) {
String str1="apple";
String str2="apple";
String str3=new String("apple");
String str4="applE";
if(str1==str2)
System.out.println("Strings are equal");
else
System.out.println("String are not Equal");
if(str1==str3)
System.out.println("Strings are equal");
else
System.out.println("String are not Equal");
if(str1.equals(str3))
System.out.println("Strings are equal");
else
System.out.println("String are not Equal");
}
}
20.Write a program to check strings are equalIgnoreCase.
class EqualsIgnoreCase {
public static void main(String[] args) {
String str1="apple";
String str2="apple";
String str3=new String("apple");
String str4="APPlE";
if(str1==str4)
System.out.println("Strings are equal");
else
System.out.println("String are not Equal");
if(str1.equalsIgnoreCase(str4))
System.out.println("Strings are equal");
else
System.out.println("String are not Equal");
if(str1.equalsIgnoreCase(str3))
System.out.println("Strings are equal");
else
System.out.println("String are not Equal");
}
}
Comments
Post a Comment