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

String

The document contains multiple Java programs that perform various string manipulations, including moving characters to the front, counting character occurrences, reversing words, removing vowels, checking for anagrams, comparing whitespace counts, calculating ASCII values, finding the longest substring without repeating characters, compressing character sequences, and checking character patterns. Each program includes a main method that prompts the user for input and displays the output. The examples provided illustrate the expected input and output for each function.

Uploaded by

Ananya Samanta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

String

The document contains multiple Java programs that perform various string manipulations, including moving characters to the front, counting character occurrences, reversing words, removing vowels, checking for anagrams, comparing whitespace counts, calculating ASCII values, finding the longest substring without repeating characters, compressing character sequences, and checking character patterns. Each program includes a main method that prompts the user for input and displays the output. The examples provided illustrate the expected input and output for each function.

Uploaded by

Ananya Samanta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

String

1. Input: Move#Hash#to#Front output: ###MoveHashtoFront


import java.util.*;
public class Main{
static String function(String str){
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(ch=='#'){
sb1.append(ch);
}else{
sb2.append(ch);
}
}
sb1.append(sb2);
return sb1.toString();
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
String ans=function(str);
System.out.println(ans);
}
}
#without StringBuilder
import java.util.*;
public class Main{
static String function(String str){
String hash="";
String rem="";
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(ch=='#'){
hash+=ch;
}else{
rem+=ch;
}
}
String res=hash+rem;
return res;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
String ans=function(str);
System.out.println(ans);
}
}
2. Input: Mango output: 10
Input : Balloon output:12

Explanation: in Mango Each letter occurs one time 2+2+2+2+2=10


in Balloon 2+2+2+1+2+1+2=12

import java.util.*;
class HelloWorld {
static int function(String str){
HashSet<Character> l1=new HashSet<>();
int totalcount=0;
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(l1.contains(ch)){
totalcount+=1;
}else{
totalcount+=2;
l1.add(ch);
}
}
return totalcount;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
int ans=function(str);
System.out.println(ans);
}
}
3. Input:Hello World output: World Hello
import java.util.*;
class HelloWorld {
static String function(String str){
String[] words=str.split(" ");
String rev=" ";
for(int i=words.length-1;i>=0;i--){
rev+=words[i];
rev+=" ";
}
return rev.trim();
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
String ans=function(str);
System.out.println(ans);
}
}
4. Return the string after removing all vowels in the string
import java.util.*;
public class Main{
static String function(String str){
StringBuilder sb = new StringBuilder();
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
int flag=1;
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'
||ch=='u'||ch=='a'||ch=='e'||ch=='i'||
ch=='o'||ch=='u'){
flag=0;
}
if(flag==1){
sb.append(ch);
}
}
return sb.toString();
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
String ans=function(str);
System.out.println(ans);
}
}
//or,
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
String ans = str.replaceAll("[AEIOUaeiou]", "");
System.out.println(ans);
}
}

5. Two strings are anagram or not


//two strings are anagram if both contains same character

//listen, silent two strings are anagrams

import java.util.*;
public class Main
{
static boolean function(String str1,String str2){
StringBuilder sb = new StringBuilder();
/*if ans is not case sensetive
str1=str1.toUpperCase();
str2=str2.toUpperCase(); */
if(str1.length()!=str2.length()){
return false;
}
char[] chararr1=str1.toCharArray();
char[] chararr2=str2.toCharArray();
Arrays.sort(chararr1);
Arrays.sort(chararr2);
boolean answer=Arrays.equals(chararr1,chararr2);
return answer;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String str1=sc.nextLine();
System.out.println("Enter another String");
String str2=sc.nextLine();
boolean ans=function(str1,str2);
System.out.println(ans);
}
}
6. You have been given two strings A and B. You have to find the difference in the
count of whitespaces in both the strings. Then you return a string even or odd
along with the difference

Input 1:
He ll o Wo rl d
Hello World

Output 1:
Even:4
import java.util.*;
public class Main
{
static String function(String str1,String str2){
StringBuilder sb = new StringBuilder();
int count1 = 0;
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) == ' ') {
count1++;
}
}
int count2 = 0;
for (int i = 0; i < str2.length(); i++) {
if (str2.charAt(i) == ' ') {
count2++;
}
}
int diff=Math.abs(count1-count2);
if(diff%2==0){
sb.append("Even");
}else{
sb.append("Odd");
}
sb.append(':');
sb.append(diff);

return sb.toString();
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String str1=sc.nextLine();
System.out.println("Enter another String");
String str2=sc.nextLine();
String ans=function(str1,str2);
System.out.println(ans);
}
}
7. Sum of Ascii value of the characters in a string
Input: Hello World
Output: 1052
import java.util.*;
public class Main{
static int function(String str){
int sum=0;
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
int c=ch;
sum=sum+c;
}
return sum;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
int ans=function(str);
System.out.println(ans);
}
}
8. Find out longest Substring and its length without repeating
characters.
Input: abcabcbb
Output:abc,3
//all the substring without repeating characters are
abc,bca,cab,abc,bc,cb,b,b

import java.util.*;
public class A{
static void function(String str){
String longestTillnow="";
String longestoverall="";
ArrayList<Character> l1=new ArrayList<>();
for(int i=0;i<str.length();i++){
for(int j=i;j<str.length();j++){
char ch=str.charAt(j);
if(l1.contains(ch)){
longestTillnow="";
l1.clear();
break;
}
l1.add(ch);
longestTillnow+=ch;
if(longestTillnow.length()>longestoverall.length()){
longestoverall=longestTillnow;
}
}
}
System.out.println("longest substring="+longestoverall);
System.out.println("length="+ longestoverall.length());
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
function(str);
}
}
9. Input: aaabbbbccddrr output: a3b4c2d2r2
import java.util.*;
public class A{
static String function(String str){
StringBuilder sb=new StringBuilder();
HashMap<Character,Integer> m1=new LinkedHashMap<>();
int val=1;
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(m1.containsKey(ch)){
m1.put(ch,m1.get(ch)+1);
}else{
m1.put(ch,val);
}
}
for(char key:m1.keySet()){
sb.append(key);
sb.append(m1.get(key));
}
return sb.toString();
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
String ans=function(str);
System.out.println(ans);
}
}
10. 4
ADAOAS [Explanation: 0,2,4 index same character so, output 1 ]
1
MADAM [0,2 index diff character]
0
ADAOPS [0,2 index same character but 0,4 index diff character]
0
aba1a0*
0
import java.util.*;
public class A{
static int function(String str){
HashSet<Character> s1= new HashSet<>();
s1.add(str.charAt(0));
for(int i=0;i<str.length();i+=2){
char ch=str.charAt(i);
if(!s1.contains(ch)){
return 0;
}
}
return 1;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine(); //to consume leftover newline
for(int i=0;i<n;i++){// for multiple testcases
String str=sc.nextLine();
int ans=function(str);
System.out.println(ans);
}

}
}
//nextInt() only reads the integer value and not the newline character \
n that occurs when you press Enter. So, when nextLine() is called right
after nextInt(), it reads this leftover newline instead of waiting for user
input.

You might also like