Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
71 views

String Programs

The document contains multiple Java code snippets that demonstrate various string manipulation techniques, including removing extra spaces from a string, finding the frequency of characters and words in a string, reversing words in a string, sorting characters in a string by descending order, and modifying a string by changing the positions of vowels and consonants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

String Programs

The document contains multiple Java code snippets that demonstrate various string manipulation techniques, including removing extra spaces from a string, finding the frequency of characters and words in a string, reversing words in a string, sorting characters in a string by descending order, and modifying a string by changing the positions of vowels and consonants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

//Removes extra space from middle of the string

public class Main {


public static void main(String[] args) {
String s = "Hello this is a long string ";
s=s.trim();
s=s+" ";//Adding 2 spaces
String wd="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ')
wd+=ch;
else
{
if(s.charAt(i-1)!=' ')
System.out.print(" "+wd);
if(s.charAt(i+1)==' ')
i++;
wd="";
}
}

}
}
OR
class Main
{
void main()
{
String s = "this is a long string ";
s=s.trim();
s=s+" ";//Adding 2 spaces
int l=s.indexOf(' ');
System.out.print(s.substring(0,l));//It will print the first word of the string
without any space
String wd="";
for(int i=l+1;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ')
wd+=ch;
else
{
if(s.charAt(i-1)!=' ')
System.out.print(" "+wd);
if(s.charAt(i+1)==' ')
i++;
wd="";
}
}
//Starting with l+1 because if we take l, two spaces will be printed between the first and the
second word
//It is so because the char before lth index won't be a space in any situation so its always
going to execute the first space hence printing an extra space
//Earlier logic was also correct but it was printing a space before the first word, this prg is
perfect now

}
}

/*
AbCd dCbA
A-2
b-2
C-2
d-2
*/
public class Main {
public static void main(String[] args) {
String s = "AbCd dCbA";
int i=0,j=0,k=0,c=0,count=0,len=s.length();
System.out.println(s);
for(i=0;i<len;i++)
{
c=0; count=0;
char ch1=s.charAt(i);

if(ch1==' ')
continue;

for(k=0;k<i;k++)
{
char ch2=s.charAt(k);
if(ch1==ch2)
c++;
}

if(c==0)
{
for(j=0;j<len;j++)
if(ch1==s.charAt(j))
count++;
}

if(count!=0)
System.out.println(ch1+" - "+count);
}

}
}

Write an program to enter a number and print the smallest digit of the number.
class SmallDigit
{
static void main(long n)
{
String s=String.valueOf(n);
int len=s.length(),i=0;
int arr[]=new int[len];
for(i=0;i<len;i++)
{
char ch=s.charAt(i);
arr[i]=(ch-48);
}

int min=arr[0];
for(i=0;i<len;i++)
{
if(arr[i]<min)
min=arr[i];
}

System.out.println("Smallest digit of the number: "+min);


}
}

Write a java Program to find the frequency of each word in the string.
This works perfect: (same logic as for counting the frequency of elements in
array,supposing that one element has been repeated later also Eg. 1,2,3,3,2,1,)
/*
String: this is a book but my book is this
this - 2
is - 2
a-1
book - 2
but - 1
my - 1
*/
class FrequencyWords
{
static void main(String s)
{
s+=" ";
System.out.println("String: "+s);
String arr[]=s.split(" ");
int len=arr.length,i=0,j=0,k=0,c=0,count=0;
for(i=0;i<len;i++)
{
count=c=0;
for(k=0;k<i;k++)
if((arr[i]).equals(arr[k]))
c++;

if(c==0)
{
for(j=0;j<len;j++)
if(arr[i].equals(arr[ j]))
count++;
System.out.println(arr[i]+" - "+count);
}
}

}
}

/* (not satisfying:keeping it for comparison)


String: this is book and book is you ha ha
this - 1
is - 2
book - 2
and - 1
book - 2
is - 2
you - 1
ha - 2
ha - 2
*/
class Main {
public static void main(String[] args) {
String s="this is book and book is you ha ha",wd1="",wd2="";
s+=" ";
int c=0;
System.out.println("String: "+s);
for(int i=0;i<s.length();i++)
{
c=0;
char ch=s.charAt(i);
if(ch!=' ')
wd1+=ch;

else
{

for(int j=0;j<s.length();j++)
{
ch=s.charAt(j);
if(ch!=' ')
wd2+=ch;
else
{
if(wd1.equals(wd2))
c++;
wd2="";
}
}

System.out.println(wd1+" - "+c);
wd1="";
}

}
}

Write a program in java to input a sentence from the user and replace its each word by an
asterisk
/*
Original String: I love my India
Modified String: * *ov* ** *ndi*
*/

public class Main {


public static void main(String s) {
String str="",wd="";
s+=" ";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ')
wd+= ch;
else
{
int len=wd.length();
if(len>1)
str+="*"+wd.substring(1,len-1)+"* ";
else
str+="* ";
wd="";
}
}
System.out.println("Original String: "+s);
System.out.println("Modified String: "+str);

}
}

Write a java program to reverse each word of a string.


class reverse_each_word
{
public static void main(String str)
{
str+=" ";
int len = str.length(), i=0, j=0;
String wd="", new_str="", new_wd="";
char ch = ' ';

for(i=0; i<len; i++)


{
ch=str.charAt(i);
if(ch!=' ')
wd+=ch;
else
{
for(j=0; j<wd.length(); j++)
{
ch = wd.charAt(j);
new_wd = ch + new_wd;
}
new_str= new_str + new_wd + " ";
wd="";
new_wd="";

}
}
System.out.println("original string: " + str.trim());
System.out.println("new string: " + new_str.trim());
}
}
OR
class Main {
void main() {
String s = "Hello baby";
s=s+" ";
String wd="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ')
wd=ch+wd;
else
{
System.out.print(wd+" ");
wd="";
}
}
}
}

Input a number and arrange its digits in descending order


/*
Original Number 9532660228
Modified Number 9866532220
*/
public class Main {
public static void main(String[] args) {
long n=9532660228L;
String s=String.valueOf(n),str="";
for( int i=9;i>=0;i--)
{
for(int j=0;j< s.length();j++)
{
char ch=s.charAt(j);
if(i==(ch-48))
str+=i;
}
}
System.out.println("Original Number "+s);
System.out.println("Modified Number "+str);
}
}

Interchange the first and last letter of all the words present in the string
/*
Original String: I get to love you
Modified String: I teg ot eovl uoy
*/
public class Main {
public static void main(String[] args) {
String s = "I get to love you",wd="",str="";
s+=" ";
int len=0,i=0;
for(i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ')
wd+=ch;
else
{
len=wd.length();
char first=wd.charAt(0), last=wd.charAt(len-1);
if(len>1)
str+=last+wd.substring(1,len-1)+first+" ";
else
str+=wd+" ";
wd="";
}
}
System.out.println("Original String: "+s);
System.out.println("Modified String: "+str);

}
}

Bring all the vowels in the word at the beginning followed by the consonants.
Example:
ORIGINAL becomes OIIARGNL
public class Main {
public static void main(String[] args) {
String s="INFINITY", vow="",cons="";
s=s.toUpperCase();
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if("AEIOU".indexOf(ch)>=0)
vow+=ch;
else
cons+=ch;
}
System.out.println("Original String: "+s);
System.out.println("Modified String: "+vow+cons);

}
}

Change the characters of String to two places ahead of it


/*
Original String: Abc3DxY1
Modified String: Cde3FzA1
*/
public class Main {
public static void main(String s) {
String str="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(Character.isLetter(ch))
{
int len=ch+2;
if(len>=65&&len<=90 || len>=97&&len<=122)
str+=(char)len;
else if(Character.isUpperCase(ch))
{
if(ch=='Y')
str+='A';
else
str+='B';
}
else
{
if(ch=='y')
str+='a';
else
str+='b';
}
}
else
str+=ch;
}
System.out.println("Original String: "+s);
System.out.println("Modified String: "+str);
}
}
Find the frequency of second string in the first one
For eg.
Str=” aabbc aa xy xyaa zz
Substr=”aa”
It should return =3

public class Main {


public static void main(String[] args) {
String s="aabbc aa xy xyaa zz",str="aa";
int l1=s.length(),l2=str.length(),c=0;

for(int i=0;i<l1-l2+1;i++) //i<=l1-l2


{
String wd=s.substring(i,i+l2);
if(wd.equals(str))
c++;
}

System.out.println("Frequency of "+str+" in "+s+" is "+c);


}
}

/*
Input a string, count frequency of all the VOWELS.
String : I love you Ami
A-1
I-1
e-1
i-1
o-2
u-1
*/
public class Main {
public static void main(String[] args) {
String s = "I love you Ami";
int i=0,c=0,len=s.length();
String vowels="AEIOUaeiou";
System.out.println("String : "+s);
for(;i<10;i++)
{
char ch=vowels.charAt(i);
c=0;
for(int j=0;j<s.length();j++)
if(s.charAt(j)==ch)
c++;
if(c>0)
System.out.print(ch+" - "+c+"\n");
}

}
}

class vowels_frequency
{
public static void main (String str)
{
int i=0, len=str.length(), count=0;
char ch=' ', CH=' ';
System.out.println("vowel \t frequency");

for(ch='A'; ch<='z'; ch++)


{
if("AEIOUaeiou".indexOf(ch)>=0)
{
for(i=0; i<len; i++)
{
CH = str.charAt(i);
if(CH==ch)
count++;
}
if(count>0)
System.out.println(ch + "\t\t" + count);
}
count=0;
}
}
}

☆ Reverse words in a string (Sample Paper 3)


public class Main {
public static void main(String[] args) {
String s = "I love you my babyyyy";
s+=" ";
String str="",wd="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ')
wd+=ch;
else
{
str=wd+" "+str; //Space to be added before
wd="";
}
}
System.out.println("Original: "+s);
System.out.println("Resultant: "+str);
}
}

public class InterchangeFirstAndLastChar {


public static void main(String[] args) {
String s="Hello";
int len=s.length();
char ch1=s.charAt(0);
char ch2=s.charAt(len-1);
System.out.println(ch2+s.substring(1,len-1)+ch1);
}
}

public class Q12_Page450 {


public static void main(String[] args) {
String s="SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE";
int len=s.length(),c=0;
for(int i=0;i<len-1;i++)
{
if(s.charAt(i)==s.charAt(i+1))
c++;
}
System.out.println(c);

}
}

/*
PRANJAL
  PRANJA
    PRANJ
      PRAN
        PRA
           PR
             P
*/
public class Main {
public static void main(String[] args) {
int space=0;
String s = "PRANJAL"; int len=s.length();
for(int i=len-1;i>=0;i --)
{
for(int k=0;k<space;k++)
System.out.print(" ");
space++;
System.out.print(s.substring(0,i+1));
System.out.println();
}
}
}

class Nested_Java
{
    static void main(String s)
    {
       int space=0;
       int len=s.length();
       for(int i=len-1;i>=0;i--)
       {
           for(int k=0;k<space;k++)
           System.out.print("\t");
           space++;
           for(int j=0;j<=i;j++)
           System.out.print(s.charAt(j)+"\t");
           System.out.println();
        }
    }
}

class PigLatin
{
    static void main(String s)
    {
        int index=0;
        for(int i=0;i<s.length();i++)
        {
            char ch=s.charAt(i);
            if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
            {
                index=i;
                break;
            }
        }
        System.out.print(s.substring(index)+s.substring(0,index)+"AY");
    }
    }
   
class Words_Digit
{
    static void main(String s)
    {
        s+=" ";
        String wd=""; int c=0;
        System.out.println(s+"\nWords with digits: ");
        for(int i=0;i<s.length();i++)
        {
            char ch=s.charAt(i);
            if(ch!=' ')
            {
            wd+=ch;
            if(ch>='0'&&ch<'9')
            c++;
            }
            else
            {
                if(c>0)
                System.out.print(wd+" , ");
                wd="";
                c=0;
            }
        }
    }
}

class CommonWords
{
    static void main(String s1,String s2)
    {
        s1+=" ";s2+=" "; String wd1="", wd2="";
        for(int i=0;i<s1.length();i++)
        {
            char ch1=s1.charAt(i);
            if(ch1!=' ')
            wd1+=ch1;
            else
            {
                for(int j=0;j<s2.length();j++)
                {
                  
                    char ch2=s2.charAt(j);
                    if(ch2!=' ')
                    wd2+=ch2;
                    else
                    {
                        if(wd1.equals(wd2))
                        System.out.print(wd1+" , ");
                        wd2="";
                    }
                }
                wd1="";
            }
        }
    }
}

class Swap
{
    static void main(String s)
    {
        int space1=s.indexOf(' ');
        int space2=s.lastIndexOf(' ');
       
System.out.println(s.substring(space2+1)+s.substring(space1,space2+1)+s.substring(0,space1))
;
    }
}

class PigLatin
{
    static void main(String s)
    {
        int i=0;
        for(i=0;i<s.length();i++)
        {
            char ch=s.charAt(i);
if("AEIOUaeiou".indexOf(ch)>=0)
break;
        }
        System.out.print(s.substring(i)+s.substring(0,i)+"AY");
    }
    }

class Q5_449
{
public static void main(String s)
{
s=s+" ";
String s1="",word="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ')
word+=ch;
else
{
for(char j='A';j<='z';j++) //this got changed
{
for(int k=0;k<word.length();k++)
{
ch=word.charAt(k);
if(j==ch)
s1+=ch;
}
}
s1+=" ";
word="";
}
}
System.out.println("String in Alphabetical Order: "+s1);
}
}

/*
Original String: abcde abc ab a
Modified String: a ab abc abcde
*/
class Q6_449
{
public static void main(String s())
{
s+=" ";
String wd="";
int i=0,max=0;
for(i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ')
wd+=ch;
else
{
if(wd.length()>max)
max=wd.length();
wd="";
}
}

System.out.println("Original String: "+s);


System.out.print("Modified String: ");
for(i=1;i<=max;i++)
{
for(int j=0;j<s.length();j++)
{
char ch=s.charAt(j);
if(ch!=' ')
wd+=ch;
else
{
if(wd.length()==i)
System.out.print(wd+" ");
wd="";
}
}
}

}
}

class Q7_449
{
    static void main(String s)
    {
        s=s.toUpperCase();
        System.out.println("Character\tFrequency");
        int count = 0;
        char ch=' ';
        for(char i='A';i<='Z';i++)
        {
            count=0;
            for(int j=0;j<s.length();j++)
            {
                ch=s.charAt(j);
                if(i==ch)
                count++;
            }
            if(count>0)
            System.out.println(i+"\t\t"+count);
        }
    }
}

import java.util.*;
public class Q8_449 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Sentence: ");
String s=sc.nextLine();
s=s+" ";
String word1="";
for(int i=0;i<s.length();i++)
{
char ch1=s.charAt(i);
if(ch1!=' ')
word1=ch1+word1;
else
{
System.out.print(word1+" ");
word1="";
}
}
}
}

class Q9_449
{
    static void main(String s)
    {
        int upper=0,lower=0,digit=0,special=0;
        for(int i=0;i<s.length();i++)
        {
            char ch=s.charAt(i);
            if(ch>='a'&&ch<='z')
            lower++;
            else if(ch>='A'&&ch<='Z')
            upper++;
            else if(ch>='0'&&ch<='9')
            digit++;
            else
            special++;
        }
        System.out.println("UpperCase Characters: "+upper);
        System.out.println("LowerCase Characters: "+lower);
        System.out.println("Digit Characters: "+digit);
        System.out.println("Special Characters: "+special);
    }
}

class Q10_449
{
   static void main(String s, String word)
    {
        s=s+" ";
        String wd="";
        int count=0;
        for(int i=0;i<s.length();i++)
        {
            char ch=s.charAt(i);
            if(ch!=' ')
            wd+=ch;     
            else
            {
            if(word.equalsIgnoreCase(wd))
            count++;
            wd="";
            }
        }
        System.out.println("Search word occurs "+count+" times");
        }
        }

class Q11_450
{
    static void main(String s)
    {
        s=s+" ";
        String longest="",word="";
        int max=0;
        for(int i=0;i<s.length();i++)
        {
            char ch=s.charAt(i);
            if(ch!=' ')
             word+=ch;
        else
        {
            if(word.length()>max)
                max=word.length();
         word="";
        }
        }
        word="";
         for(int i=0;i<s.length();i++)
        {
            char ch=s.charAt(i);
            if(ch!=' ')
             word+=ch;
             else
             {
                 if(word.length()==max)
                 longest+=word+" ";
                 word="";
             }
        }
   
    System.out.println("Biggest Word: "+longest);
    System.out.println("Length of longest Word: "+max);
}
}

public class Q12_450


{
public static void main(String s)
{
s=s.toUpperCase();
s=s+" ";
String word="";int count=0;
for(int i=0;i<s.length()-1;i++)
{
char ch1=s.charAt(i);
char ch2=s.charAt(i+1);
if(ch1==ch2)
count++;
}
System.out.println("Number of Paired Words: "+count);
}
}

public class Q19_Page398 {


public static void main(String[] args) {
long n=9532660228L;
int c=0;
String s=String.valueOf(n);
for(int i=0;i<10;i++)
{
c=0;
for(int j=0;j<10;j++)
{
char ch=s.charAt(j);
if((ch-48)==i)
c++;
}
if(c!=0)
System.out.println("Frequency of "+i+" is "+c);
}
}
}

You might also like