String Programs
String Programs
}
}
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];
}
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);
}
}
}
}
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*
*/
}
}
}
}
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="";
}
}
}
}
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);
}
}
/*
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");
}
}
/*
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="";
}
}
}
}
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);
}
}