0% found this document useful (0 votes)
7 views

String

Uploaded by

ANITHARANI K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

String

Uploaded by

ANITHARANI K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Replace Case:

import java.util.*;

class HelloWorld {
public static void main(String[] args) {
String a = "HelLO";
String output = "";

for (int i = 0; i < a.length(); i++) {


char c = a.charAt(i);
if (c >= 'a' && c <= 'z') {
// Convert lowercase to uppercase by subtracting 32
output += (char) (c - 32);
} else if (c >= 'A' && c <= 'Z') {
// Convert uppercase to lowercase by adding 32
output += (char) (c + 32);
} else {
// If the character is not an alphabet, keep it unchanged
output += c;
}
}
System.out.print(output);
}
}

Remove vowel:

import java.util.*;

class HelloWorld {
public static void main(String[] args) {
String a = "HelLO";

for (int i = 0; i < a.length(); i++) {


char c = a.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
continue;
} else {
System.out.print(c);
}
}
}
}

Palindrome
import java.util.*;

class HelloWorld {
public static void main(String[] args) {
String X = "arorA";
String rev="";
String a=X.toLowerCase();
for(int i=a.length()-1;i>=0;i--)
{
rev=rev+a.charAt(i);
}
if(a.equals(rev))
{
System.out.print("Palindrome");
}
else
{
System.out.print("Not a Palindrome");
}
}
}

Reverse String

import java.util.*;

class HelloWorld {
public static void main(String[] args) {
String a = "anitharani";
String rev="";

for(int i=a.length()-1;i>=0;i--)
{
rev=rev+a.charAt(i);
}
System.out.print(rev);

}
}

Remove Alphabets

import java.util.*;

class HelloWorld {
public static void main(String[] args) {
String a = "AniTha";

for (int i = 0; i < a.length(); i++) {


char c = a.charAt(i);
if (c>='A'&&c<='Z')
{
continue;
} else {
System.out.print(c);
}
}
}
}

String frequency

public class CharacterFrequency {


public static void main(String[] args) {
String str = "Malayalam";

int[] frequency = new int[256];

for (int i = 0; i < str.length(); i++) {


char c = str.charAt(i);
frequency[c]++;
}

System.out.println("Character frequencies:");
for (int i = 0; i < frequency.length; i++) {
if (frequency[i] != 0) {
System.out.println("'" + (char) i + "': " + frequency[i]);
}
}
}
}

Anagram:

import java.util.Arrays;

public class AnagramCheck {


public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();

if (str1.length() != str2.length()) {
System.out.println("The strings '" + str1 + "' and '" + str2 + "' are not
anagrams.");
return;
}

char[] charArray1 = str1.toCharArray();


char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);

if (Arrays.equals(charArray1, charArray2)) {
System.out.println("The strings '" + str1 + "' and '" + str2 + "' are anagrams.");
} else {
System.out.println("The strings '" + str1 + "' and '" + str2 + "' are not
anagrams.");
}
}
}

String Replace

public class ReplaceWordInString {


public static void main(String[] args) {
String sentence = "Hello AR, Nice to meet you";
String oldWord = "AR";
String newWord = "Anitha";

// Replace the old word with the new word in the sentence
String modifiedSentence = sentence.replace(oldWord, newWord);

// Print the modified sentence


System.out.println("Original sentence: " + sentence);
System.out.println("Modified sentence: " + modifiedSentence);
}
}

You might also like