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

All Sring Code

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)
22 views5 pages

All Sring Code

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
You are on page 1/ 5

1 .

reverse a string

import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
char str1[]=str.toCharArray();
for(int i=str1.length-1;i>=0;i--){
System.out.print(str1[i]);
}
}
}

2. palendrome or not

import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
char str1[]=str.toCharArray();
String rev="";
for(int i=str1.length-1;i>=0;i--){
rev+=str1[i];
}
if(str.equals(rev)){
System.out.print("TRUE");
}
else{
System.out.print("FALSE");
}
}
}

3. Captalize string

import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
System.out.print(str.toUpperCase());
}
}

4. captilized each word

import java.util.*;

class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String str1="";
str=" "+str;
for(int i=0;i<str.length();i++){
str1=str1+str.charAt(i);
if(i<str.length()-1 && str.charAt(i)==' ' && str.charAt(i+1)!=' ')
{
str1+=Character.toUpperCase(str.charAt(i+1));
i++;
}
}

System.out.print(str1);

}
}

6. tongle

import java.util.*;

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

if(i%2==0)
{
str1+=Character.toUpperCase(str.charAt(i));

}
else if(i%2!=0){
str1=str1+Character.toLowerCase(str.charAt(i));

}
}

System.out.print(str1);

}
}

5. reverse each word

import java.util.*;

class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String [] str1=str.split(" ");
for(int i=0;i<str1.length;i++)
{
StringBuilder str2 = new StringBuilder(str1[i]);
System.out.print(str2.reverse()+" ");
}

}
}

6.toggle each word

import java.util.*;

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

String []str2=str.split(" ");


for(int i=0;i<str2.length;i++){
String first=str2[i].substring(0,1);
String rest=str2[i].substring(1);
str1+=first.toLowerCase()+rest.toUpperCase()+" ";
}
System.out.print(str1);

}
}

7. anagram

import java.util.*;

class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str1=sc.nextLine();
String str2=sc.nextLine();
char []s1=str1.toCharArray();
char []s2=str2.toCharArray();
Arrays.sort(s1);
Arrays.sort(s2);
int flag =0;
if(s1.length==s2.length)
{
for(int i=0;i<s1.length;i++)
{
if(s1[i]!=s2[i])
{
System.out.print("no");
flag=1;
break;

}
}
if(flag==0)
System.out.print("yes");
}
else
{
System.out.print("no");

}
}
}

8. rotation

// Online Java Compiler


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

String s1= str+str;


int k=str.length();
String s2 = s1.substring(n,n+k);
System.out.print(s2);

}
}

9. remove white space

// Online Java Compiler


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

}
}

10. no. of occurence


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

}
}

}
}

11. non empty substring

// Online Java Compiler


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

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

for(int j=i+1;j<str.length()+1;j++){
System.out.println(str.substring(i, j));
}
}

}
}

You might also like