0% found this document useful (0 votes)
7 views5 pages

Java String Manipulation Examples

The document contains 11 code snippets in Java that demonstrate various string manipulation techniques: 1. Reversing a string 2. Checking if a string is a palindrome 3. Converting a string to uppercase 4. Capitalizing the first letter of each word in a string 5. Reversing each word in a string 6. Toggling the case of each letter in a string 7. Checking if two strings are anagrams 8. Checking string rotation 9. Removing whitespace from a string 10. Counting the occurrences of each letter in a string 11. Printing all non-empty substrings of a string

Uploaded by

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

Java String Manipulation Examples

The document contains 11 code snippets in Java that demonstrate various string manipulation techniques: 1. Reversing a string 2. Checking if a string is a palindrome 3. Converting a string to uppercase 4. Capitalizing the first letter of each word in a string 5. Reversing each word in a string 6. Toggling the case of each letter in a string 7. Checking if two strings are anagrams 8. Checking string rotation 9. Removing whitespace from a string 10. Counting the occurrences of each letter in a string 11. Printing all non-empty substrings of a string

Uploaded by

rohit1202.be21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

1 .

reverse a string

import [Link].*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();
char str1[]=[Link]();
for(int i=[Link]-1;i>=0;i--){
[Link](str1[i]);
}
}
}

2. palendrome or not

import [Link].*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();
char str1[]=[Link]();
String rev="";
for(int i=[Link]-1;i>=0;i--){
rev+=str1[i];
}
if([Link](rev)){
[Link]("TRUE");
}
else{
[Link]("FALSE");
}
}
}

3. Captalize string

import [Link].*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();
[Link]([Link]());
}
}

4. captilized each word

import [Link].*;

class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();
String str1="";
str=" "+str;
for(int i=0;i<[Link]();i++){
str1=str1+[Link](i);
if(i<[Link]()-1 && [Link](i)==' ' && [Link](i+1)!=' ')
{
str1+=[Link]([Link](i+1));
i++;
}
}

[Link](str1);

}
}

6. tongle

import [Link].*;

class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();
String str1="";
str=" "+str;
for(int i=0;i<[Link]();i++){

if(i%2==0)
{
str1+=[Link]([Link](i));

}
else if(i%2!=0){
str1=str1+[Link]([Link](i));

}
}

[Link](str1);

}
}

5. reverse each word

import [Link].*;

class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();
String [] str1=[Link](" ");
for(int i=0;i<[Link];i++)
{
StringBuilder str2 = new StringBuilder(str1[i]);
[Link]([Link]()+" ");
}

}
}

[Link] each word

import [Link].*;

class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();
String str1=" ";

String []str2=[Link](" ");


for(int i=0;i<[Link];i++){
String first=str2[i].substring(0,1);
String rest=str2[i].substring(1);
str1+=[Link]()+[Link]()+" ";
}
[Link](str1);

}
}

7. anagram

import [Link].*;

class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str1=[Link]();
String str2=[Link]();
char []s1=[Link]();
char []s2=[Link]();
[Link](s1);
[Link](s2);
int flag =0;
if([Link]==[Link])
{
for(int i=0;i<[Link];i++)
{
if(s1[i]!=s2[i])
{
[Link]("no");
flag=1;
break;

}
}
if(flag==0)
[Link]("yes");
}
else
{
[Link]("no");

}
}
}

8. rotation

// Online Java Compiler


// Use this editor to write, compile and run your Java code online
import [Link].*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();
int n=[Link]();

String s1= str+str;


int k=[Link]();
String s2 = [Link](n,n+k);
[Link](s2);

}
}

9. remove white space

// Online Java Compiler


// Use this editor to write, compile and run your Java code online
import [Link].*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();
[Link]([Link](" ",""));

}
}

10. no. of occurence


// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import [Link].*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();
for(char ch='a';ch<='z';ch++){
int count =0;
for(int i=0;i<[Link]();i++){
if(ch==[Link](i)){
count++;
}
}
int k=0;
if(count!=0){
[Link](ch+""+count);

}
}

}
}

11. non empty substring

// Online Java Compiler


// Use this editor to write, compile and run your Java code online
import [Link].*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str=[Link]();

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

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

}
}

Common questions

Powered by AI

The character frequency analysis in Java involves iterating over each letter of the alphabet. For each letter, a nested loop is used to count occurrences in the input string. The results, showing each character and its count, are printed only if the count is greater than zero. This approach ensures the frequency count of each character in the input string .

The method to capitalize the first letter of each word in a string involves using a loop to iterate through each character of the string. The logic is to check if a character is a space followed by a non-space character, then capitalize the non-space character. This is done by concatenating the character with its uppercase version using `Character.toUpperCase()` .

The technique to remove all whitespace from a Java string utilizes the `replaceAll()` method with a regex pattern matching spaces. Specifically, `str.replaceAll(" ", "")` removes all space characters from the string, resulting in a continuous sequence of non-space characters left in the resulting string .

Generating non-empty substrings in Java involves using two nested loops. The outer loop starts from the beginning and goes to the end of the string, while the inner loop begins from the next character of the outer index and goes to the length of the string plus one. This nested iteration allows all possible substrings to be printed by capturing characters between index ranges determined by the loops .

To reverse each word in a sentence using Java, you can split the sentence into words, reverse each word individually, and then print the reversed words with spaces. Specifically, this involves using `String.split()` to obtain each word, then using `StringBuilder.reverse()` to reverse each word, and finally concatenating the reversed words to form the output .

To check if a string is a palindrome in Java, you can reverse the string and compare it with the original. This involves taking the input string, converting it to a character array, reversing the character array, and then checking if the reversed string matches the original. If they are equal, the string is a palindrome; otherwise, it is not .

To perform an n-position rotation of a string in Java, concatenate the string with itself, and then extract a substring starting at the n-th position with a length equal to the original string. This leverages string duplication and substring extraction to simulate the rotation effect .

The Java program toggles the case of each word by splitting the input string into words, then transforming each word such that the first letter is lowercase and the rest of the letters are uppercase. This is accomplished using `substring()` for string manipulation and `toLowerCase()` and `toUpperCase()` methods to adjust the cases of the first and rest of the letters, respectively .

To determine if two strings are anagrams in Java, the strategy involves converting both strings to character arrays, sorting these arrays, and then comparing them. If the sorted arrays are identical, the strings are anagrams. This approach ensures that the characters and their frequencies match in both strings .

Character toggling at even and odd indexes involves iterating over the string, checking the index of each character, and applying different case transformations based on its parity. Even index characters are converted to uppercase, while odd index characters are converted to lowercase, using methods `toUpperCase()` and `toLowerCase()`, respectively, allowing systematic toggling throughout the string .

You might also like