0% found this document useful (0 votes)
40 views15 pages

Java String Manipulation Techniques

The document contains Java programs that demonstrate various string manipulation techniques, including reversing a string, checking for palindromes, determining anagrams, counting vowels and consonants, and finding the longest substring without repeating characters. It provides both implementations that use a scanner for user input and those that use hardcoded strings. Additionally, it includes methods for identifying duplicate characters, the first non-repeating character, the longest word in a string, counting words, checking string rotations, and removing spaces from a string.
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)
40 views15 pages

Java String Manipulation Techniques

The document contains Java programs that demonstrate various string manipulation techniques, including reversing a string, checking for palindromes, determining anagrams, counting vowels and consonants, and finding the longest substring without repeating characters. It provides both implementations that use a scanner for user input and those that use hardcoded strings. Additionally, it includes methods for identifying duplicate characters, the first non-repeating character, the longest word in a string, counting words, checking string rotations, and removing spaces from a string.
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

WITHOUT SCANNER METHOD

Reverse a String

Question: Write a Java program to reverse a given string.

public class ReverseString {

public static void main(String[] args) {

String str = "hello";

String reversed = "";

for (int i = [Link]() - 1; i >= 0; i--) {

reversed += [Link](i);

[Link]("Reversed String: " + reversed);

}
2️⃣ Check for Palindrome

Question: Check if the given string is a palindrome or not.

public class PalindromeString {

public static void main(String[] args) {

String str = "madam";

String rev = new StringBuilder(str).reverse().toString();

if ([Link](rev))

[Link]("Palindrome");

else

[Link]("Not Palindrome");

}
3️⃣ Check if Two Strings are Anagrams

Question: Determine if two strings are anagrams.

import [Link];

public class AnagramCheck {

public static void main(String[] args) {

String s1 = "listen";

String s2 = "silent";

char[] a = [Link]();

char[] b = [Link]();

[Link](a);

[Link](b);

if ([Link](a, b))

[Link]("Anagram");

else

[Link]("Not Anagram");

}
Count Vowels and Consonants:

Question: Count the number of vowels and consonants in a string.

public class CountVowelsConsonants {

public static void main(String[] args) {

String str = "welcome";

str = [Link]();

int vowels = 0, consonants = 0;

for (int i = 0; i < [Link](); i++) {

char ch = [Link](i);

if ("aeiou".indexOf(ch) != -1)

vowels++;

else if (ch >= 'a' && ch <= 'z')

consonants++;

[Link]("Vowels: " + vowels);

[Link]("Consonants: " + consonants);

}
Longest Substring Without Repeating Characters

Question: Find the length of the longest substring without repeating characters.

import [Link];

public class LongestUniqueSubstring {

public static void main(String[] args) {

String s = "abcabcbb";

int maxLength = 0;

for (int i = 0; i < [Link](); i++) {

HashSet<Character> set = new HashSet<>();

for (int j = i; j < [Link](); j++) {

if ([Link]([Link](j)))

break;

[Link]([Link](j));

maxLength = [Link](maxLength, j - i + 1);

[Link]("Length of Longest Unique Substring: " + maxLength);

}
With SCANNER:

Reverse a String

import [Link];

public class ReverseString {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");

String str = [Link]();

String rev = "";

for(int i = [Link]()-1; i >= 0; i--) {

rev += [Link](i);

[Link]("Reversed String: " + rev);

}
Check Palindrome String

import [Link];

public class PalindromeString {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");

String str = [Link]();

String rev = "";

for(int i = [Link]()-1; i >= 0; i--) {

rev += [Link](i);

if([Link](rev))

[Link]("It is a Palindrome.");

else

[Link]("Not a Palindrome.");

}
Count Vowels and Consonants

import [Link];

public class CountVowelsConsonants {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");

String str = [Link]().toLowerCase();

int vowels = 0, consonants = 0;

for(int i = 0; i < [Link](); i++) {

char ch = [Link](i);

if([Link](ch)) {

if("aeiou".indexOf(ch) != -1)

vowels++;

else

consonants++;

[Link]("Vowels: " + vowels);

[Link]("Consonants: " + consonants);

}
Check Anagram

import [Link];

import [Link];

public class AnagramCheck {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first string: ");

String s1 = [Link]().toLowerCase();

[Link]("Enter second string: ");

String s2 = [Link]().toLowerCase();

char[] a1 = [Link]("\\s", "").toCharArray();

char[] a2 = [Link]("\\s", "").toCharArray();

[Link](a1);

[Link](a2);

if([Link](a1, a2))

[Link]("Strings are Anagrams.");

else

[Link]("Strings are not Anagrams.");

}
5️⃣ Find Duplicate Characters

import [Link];

public class DuplicateCharacters {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");

String str = [Link]().toLowerCase();

int count;

char[] chars = [Link]();

[Link]("Duplicate characters:");

for(int i = 0; i < [Link]; i++) {

count = 1;

if(chars[i] == '0' || chars[i] == ' ')

continue;

for(int j = i+1; j < [Link]; j++) {

if(chars[i] == chars[j]) {

count++;

chars[j] = '0';

if(count > 1)

[Link](chars[i]);

}
First Non-Repeating Character

import [Link];

public class FirstNonRepeating {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");

String str = [Link]();

boolean found = false;

for(int i = 0; i < [Link](); i++) {

char ch = [Link](i);

if([Link](ch) == [Link](ch)) {

[Link]("First non-repeating character: " + ch);

found = true;

break;

if(!found)

[Link]("No unique character found.");

}
Longest Word in a String

import [Link];

public class LongestWord {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a sentence: ");

String[] words = [Link]().split(" ");

String longest = "";

for(String w : words) {

if([Link]() > [Link]())

longest = w;

[Link]("Longest word: " + longest);

}
Count Words in a String

import [Link];

public class CountWords {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a sentence: ");

String str = [Link]().trim();

String[] words = [Link]("\\s+");

[Link]("Number of words: " + [Link]);

}
Check if One String is Rotation of Another

import [Link];

public class StringRotation {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first string: ");

String s1 = [Link]();

[Link]("Enter second string: ");

String s2 = [Link]();

if([Link]() == [Link]() && (s1 + s1).contains(s2))

[Link]("One string is rotation of another.");

else

[Link]("Not a rotation.");

}
Remove Spaces from a String

import [Link];

public class RemoveSpaces {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");

String str = [Link]();

str = [Link]("\\s+", "");

[Link]("String without spaces: " + str);

You might also like