0% found this document useful (0 votes)
9 views36 pages

MATERIAL FOR STRING REVISION

The document contains a list of 25 Java programs that perform various string manipulations, including counting vowels and consonants, toggling case, converting strings to Pig Latin, and checking for palindromes. Each program is presented with its code and a brief description of its functionality. The programs cover a wide range of string processing tasks, demonstrating fundamental programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views36 pages

MATERIAL FOR STRING REVISION

The document contains a list of 25 Java programs that perform various string manipulations, including counting vowels and consonants, toggling case, converting strings to Pig Latin, and checking for palindromes. Each program is presented with its code and a brief description of its functionality. The programs cover a wide range of string processing tasks, demonstrating fundamental programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

9FINAL LIST OF STRING PROGRAMS TO BE REVISED

1. ACCEPT A STRING COUNT VOWELS, CONSONANTS AND WHITESPACES


PRESENT IN THE STRING.
import java.util.*;
class print
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
int vowel=0,cons=0,ws=0;
String x = sc.nextLine();
for(int i=0;i<x.length();i++)
{
char ch = x.charAt(i);
if(Character.isLetter(ch)==true)
{
if("AEIOUaeiou".indexOf(ch)!=-1)
vowel++;
else
cons++;
}
if(Character.isWhitespace(ch)==true)
ws++;
}
System.out.println("Number of vowels"+vowel);
System.out.println("Number of consonants"+cons);
System.out.println("Number of whitespaces"+ws);
}
}
2. CONCATENATE STARTING LETTER OF EACH WORD.
mangoes are delivered after midday
output
madam
import java.util.*;
class madam
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
String g=sc.nextLine();
g=" "+g;String str="";
for(int i=0;i<g.length();i++)
{
char ch=g.charAt(i);
if(ch==' ')
str=str+g.charAt(i+1);
}
System.out.println(str);
}
}
3. COUNT UPPERCASE, LOWERCASE, WHITESPACES, SPECIAL SYMBOLS
import java.util.*;
class count
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
String g=sc.nextLine();
int i;
int up=0,lo=0,di=0,sp=0,wsp=0;
for(i=0;i<g.length();i++)
{
char ch=g.charAt(i);`
if(Character.isUpperCase(ch)==true)
up=up+1;
else if(Character.isLowerCase(ch)==true)
lo=lo+1;
else if(Character.isDigit(ch)==true)
di=di+1;
else if(Character.isWhitespace(ch)==true)
wsp=wsp+1;

else
sp=sp+1;
}
System.out.println("Number of UpperCase letters "+up);
System.out.println("Number of LowerCase letters "+lo);
System.out.println("Number of White spaces "+wsp);
System.out.println("Number of digits "+di);
System.out.println("Number of special symbols "+sp);
}
}
4. ACCEPT A STRING IN LOWER CASE REPLACE ALL THE VOWELS BY THE
CHARACTER '*' AND CREATE A NEW STRING
import java.util.*;
class replacevowels
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
String g=sc.next();
g=g.toLowerCase();
String st="";int i;
for(i=0;i<g.length();i++)
{
char ch=g.charAt(i);
if("AEIOUaeiou".indexOf(ch)!=-1)
st=st+'*';
else
st=st+ch;
}
System.out.println(st);
}
}
5. ACCEPT A STRING AND CONVERT ALL CHARACTERS INTO ITS
OPPOSITE CASE
TOGGLE CASE
import java.util.*;
class togglecase
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
String g=sc.nextLine();
String st="";int i;
for(i=0;i<g.length();i++)
{
char ch=g.charAt(i);
if(Character.isLowerCase(ch)==true)
st=st+Character.toUpperCase(ch);
else if(Character.isUpperCase(ch)==true)
st=st+Character.toLowerCase(ch);
else
st=st+ch;
}
System.out.println(st);
}
}
6.PATHNAME TO BE SEPARATED
c:jdk\bin\myfolder\flower.jpg
output:
path name: c:jdk\bin\myfolder
file name: flower
extension: jpg
import java.util.*;
class flower
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String m=sc.nextLine();
int p=m.lastIndexOf(‘\\’);
String a=m.substring(0,p);
String b=m.substring(p+1);
System.out.println(a);
int k=b.indexOf('.');
System.out.println(b.substring(0,k));
System.out.println(b.substring(k+1));
}
}
7. PIGLATIN FORM
import java.util.*;
class PIGLATIN
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
String st=sc.next(); //project
int i;
for( i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
break;
}
String n=st.substring(i);
String m=st.substring(0,i);
System.out.println(n+m+”ay”);
}
}
8. ACCEPT A STRING AND CONVERT THE STARTING LETTER OF EACH
WORD IN CAPITALS (CAPITALISE EACH WORD)
import java.util.*;
class madam
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
String g=sc.nextLine();
g=" "+g;String str="";
for(int i=0;i<g.length();i++)
{
char ch=g.charAt(i);
if(ch==' ')
{
str=str+' '+Character.toUpperCase(g.charAt(i+1));
i++;
}
else
str=str+ch;
}
System.out.println(str);
}
}
9. PALINDROME OR SPECIAL WORD
import java.util.*;
class pali
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string to be checked");
String m=sc.next();
String wd="";
for(int i=0;i<m.length();i++)
{
char ch=m.charAt(i);
wd=ch+wd;
}
if(wd.equals(m)==true)
System.out.println("Palindrome and special word");
else if(m.charAt(0)==m.charAt(m.length()-1))
System.out.println("Not a Palindrome but special word");
else
System.out.println("Neither Palindrome nor special word");
}
}
10. PRINTING SAME LETTER PAIR
import java.io.*;
class words7
{
public static void main(String args[])
{

String g="RABBIT CARROT";char ch1,ch2;


int i;
for(i=0;i<g.length()-1;i++)
{
ch1=g.charAt(i);
ch2=g.charAt(i+1);
if(ch1==ch2)
System.out.println(ch1+" "+ch2);(
}
}
}
11. INPUT: BEAUTIFUL BEAUTIES
OUTPUT:
E A
A U
E A
A U
I E
import java.io.*;
class words6
{
public static void main(String args[])throws IOException
{
String g="BEAUTIFUL BEAUTIES";
char ch1,ch2;
int i;
for(i=0;i<g.length()-1;i++)
{
ch1=g.charAt(i);
ch2=g.charAt(i+1);
if((“AEIOUaeiou”.indexOf(ch1)!=-1) && (“AEIOUaeiou”.indexOf(ch2)!=-1))
System.out.println(ch1+" "+ch2);
}
}
}
12.TO PRINT INITIALS AND SURNAME
class surname
{
public static void main()
{
System.out.println("Please enter a string");
String g="MOHANDAS KARAMCHAND GANDHI";
String st="",str="";
int k=g.lastIndexOf(' ');
String s1=g.substring(0,k);
String s2=g.substring(k+1);
s1=" "+s1;
for(int i=0;i<s1.length();i++)
{
char ch=s1.charAt(i);
if(ch==' ')
str=str+s1.charAt(i+1)+".";
}
System.out.println(str+s2);
}
}
13.PRINT AND COUNT THE VOWELS PRESENT IN A WORD
import java.util.*;
class words4
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
String g=sc.nextLine();
g=g.toLowerCase();
int i;int c=0;
for(i=0;i<g.length();i++)
{
char ch=g.charAt(i);
if(Character.isLetter(ch)==true)
{
if(“AEIOUaeiou”.indexOf(ch)!=-1)
{
System.out.println(ch);
c++;
}
}
}
System.out.println(c);
}
}
14. MAGIC STRING (HAS ATLEAST ONE CONSECUTIVE LETTER PAIR)
import java.util.*;
class words4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
String g=sc.nextLine();
int f=0;
for(int i=0;i<g.length()-1;i++)
{
char ch1=g.charAt(i);
char ch2=g.charAt(i+1);
if(ch2-ch1==1)
f=1;
}
if(f==1)
System.out.println("Magic string");
else
System.out.println("Not a magic string");
}
}
15. input :: c o m p u t e r z
output:: d q n q w u g s a
import java.util.*;
class vowel
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
String g=sc.nextLine();
g=g.toLowerCase();
String a="";int i;
for(i=0;i<g.length();i++)
{
char ch=g.charAt(i);
if((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'))
{
ch=(char)(ch+2);
a=a+ch;
}
else
{
int t=ch+1;
if(t>122)
t=t-26;
a=a+(char)t;
}
}
System.out.println(a);
}
}
16. Accept a word and a character from the user. Count and print the
frequency of the character accepted from the user in the given word. Print the
word along with the frequency of the given letter.
import java.util.*;
class countchar
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int count=0;
System.out.println("Please enter a string");
String g=sc.nextLine();
System.out.println("enter a character");
char c=sc.next().charAt(0);
for(int i=0;i<g.length();i++)
{
char ch=g.charAt(i);
if(ch==c)
count++;
}
System.out.println(g+" "+count);
}
}
17. Accept a string and convert into uppercase
if the word is of odd length print the middle
character and if the string is of even length divide the string
into two equal half and print both.
example1: SAMPLE(even length)
SAM
PLE
example 2: MADAM(odd length)
D
import java.util.*;
class question2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String st=sc.next();
int n;
int t=st.length();
if(t%2==0)
{
n=t/2;
System.out.println(st.substring(0,n));
System.out.println(st.substring(n));
}
else
{
n=t/2;
System.out.println(st.charAt(n));
}
}
}
18. HISTORY
SCIENCE
OUTPUT: HEICSNTEOIRCYS
import java.util.*;
class question3
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String m=sc.next();
String n=sc.next();
String st="";
if(m.length()==n.length())
{
int k=m.length()-1;
for(int i=0;i<m.length();i++,k--)
{
char ch1=m.charAt(i);
char ch2=n.charAt(k);
st=st+ch1+ch2;
}
System.out.println(st);
}
else
System.out.println("Both strings are not of same length");
}
}
19. ACCEPT A STRING AND CONVERT INTO LOWER CASE PRINT THE
LETTER WITH MAXIMUM ASCII VALUE AND PRINT THE LETTER WITH
MINIMUM ASCII VALUE.
camera
lowest letter is a
highest letter is r
import java.util.*;
class question4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String st=sc.next();
char max=st.charAt(0);
char min=st.charAt(0);
for(int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if(ch>max)
max=ch;
if(ch<min)
min=ch;
}
System.out.println("The letter with the maximum ascii value:"+max);
System.out.println("The letter with the minimum ascii value:"+min);
}
}
20. COUNT THE NUMBER OF WORDS AND ALPHABETS
import java.util.*;
class COUNT2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence");
String n=sc.nextLine();
int count=0,letter=0;
for(int i=0;i<n.length();i++)
{
char ch=n.charAt(i);
if(ch==' ')
count++;
if(Character.isLetter(ch)==true)
letter++;
}
System.out.println("Number of words:"+(count+1));
System.out.println("Number of alphabets:"+letter);
}
}
21. input: flower
output: rlowef
INTERCHANGE THE FIRST AND LAST LETTERS OF A WORD REMAINING
LETTERS REMAIN THE SAME
import java.util.*;
class CHANGE
{
public static void main()
{
String s=sc.next();
char ch1=s.charAt(0);
char ch2=s.charAt(s.length()-1);
String str=s.substring(1,s.length()-1);
System.out.println(ch2+str+ch1);
}
}
22. REMOVING DUPLICATE LETTERS
import java.util.*;
class REMOVE_DUPLICATE_LETTERS
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String m="exampledata";
String st="";
for(int i=0;i<m.length();i++)
{
char ch=m.charAt(i);
if(st.indexOf(ch)==-1)
st=st+ch;
}
System.out.println(st);
}
}
23. "bangbang"
TO CHECK WHETHER THE STRING IS A HALF PALINDROME
import java.util.*;
class HALFPAL
{
public static void main()
{
String s=sc.next();
s=s.toLowerCase();
int n=s.length()/2;
String k=s.substring(0,n);
String k1=s.substring(n);
if(k.equals(k1)==true)
System.out.println("HalfPal");
else
System.out.println("Not HalfPal");
24. PRINT THE FREQUENCY OF LETTERS IN A WORD IN ALPHABETICAL
ORDER
class alphabetical
{
public static void main()
{
String m="COMPUTER HARDWARE";
int c;char ch;
for(char i='A';i<='Z';i++)
{
c=0;
for(int j=0;j<m.length();j++)
{
ch=m.charAt(j);
if(ch==i)
c++;
}
if(c>0)
System.out.println(i+" "+c);
}

}
}
25. ARRANGE THE LETTERS IN ALPHABETICAL ORDER
class alphabetical
{
public static void main()
{
String m="COMPUTER HARDWARE";
String st=””;
for(char i='A';i<='Z';i++)
{
for(int j=0;j<m.length();j++)
{
char ch=m.charAt(j);
if(ch==i)
st=st+ch;
}
}
System.out.println(st);
}
}
26. ORIGINAL STRING::RABBIT AND CARROT
MODIFIED STRING::RAZIT AND CAZOT
class RABBIT
{
public static void main()
{
String g="RABBIT AND CARROT";
g=g+" ";
char ch1,ch2;String st="";
int i;
for(i=0;i<g.length()-1;i++)
{
ch1=g.charAt(i);
ch2=g.charAt(i+1);
if(ch1==ch2)
{
st=st+'Z';
i++;
}
else
st=st+ch1;
}
st=st+g.charAt(g.length()-1);
System.out.println("ORIGINAL STRING::"+g);
System.out.println("MODIFIED STRING::"+st);
}
}
27. ORIGINAL STRING::RABBIT AND CARROT
MODIFIED STRING::RAIT AND CAOT
class RABBIT
{
public static void main()
{
String g="RABBIT AND CARROT";
g=g+" ";
char ch1,ch2;String st="";
int i;
for(i=0;i<g.length()-1;i++)
{
ch1=g.charAt(i);
ch2=g.charAt(i+1);
if(ch1==ch2)
i++;
else
st=st+ch1;
}
st=st+g.charAt(g.length()-1);
System.out.println("ORIGINAL STRING::"+g);
System.out.println("MODIFIED STRING::"+st);
}
}
28. Enter any two strings
GOOD
LUCK
ORIGINAL STRING::GOOD AND LUCK
CREATED NEW STRING::GLOUOCDK
import java.util.*;
class GOODLUCK
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any two strings");
String m=sc.next();
String n=sc.next();
char ch1,ch2;String st="";
int i;
if(m.length()==n.length())
{
for(i=0;i<m.length();i++)
{
ch1=m.charAt(i);
ch2=n.charAt(i);
st=st+ch1+ch2;
}
System.out.println("ORIGINAL STRING::"+m+" AND "+n);
System.out.println("CREATED NEW STRING::"+st);
}
else
System.out.println("BOTH STRINGS ARE OF NOT SAME LENGTH");
}
}
29. Write a program in java to accept a string and convert into uppercase and
perform the following. A class Encode has been defined to replace only the vowels in
a word by the next corresponding vowel and form a new word.
i.e., A → E, E → I, I → O, O → U, U → A
example: input: INSTITUTION
output: ONSTOTATOUN
import java.util.*;
class NEXTVOWEL
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the string..");
String s=sc.nextLine().toUpperCase();
String st="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch=='A')
st=st+'E';
else if(ch=='E')
st=st+'I';
else if(ch=='I')
st=st+'O';
else if(ch=='O')
st=st+'U';
else if(ch=='U')
st=st+'A';
else
st=st+ch;
}
System.out.println(st);
}
}
Pattern Printing
1. I
IC
ICS
ICSE

2. COMPUTER
COMPUTE
COMPUT
COMPU
COMP
COM
CO
C
3. P
PR
PRO
PROG
PROGR
PROGRA
PROGRAM
import java.io.*;
class pattern1
{
public static void main()
{
String g="ICSE";
int i;
for(i=1;i<=g.length();i++)
System.out.println(g.substring(0,i));
}
}
import java.io.*;
class pattern2
{
public static void main()
{
String g="COMPUTER";
int i;
for(i=g.length();i>=1;i--)
System.out.println(g.substring(0,i));
}
}
import java.io.*;
class pattern3
{
public static void main()
{
String g="PROGRAM";
int i;
for(i=1;i<=g.length();i++)
System.out.println(g.substring(0,i));
}
}
PATTERN 4:
S
US
PUS
MPUS
AMPUS
CAMPUS
import java.util.*;
class PATTERN
{
public static void main()
{
String m="CAMPUS";
for(int i=m.length()-1;i>=0;i--)
System.out.println(m.substring(i));
}
}
PATTERN 5:
CAMPUS
AMPUS
MPUS
PUS
US
S
import java.util.*;
class PATTERN
{
public static void main()
{
String m="CAMPUS";
for(int i=m.length()-1;i>=0;i--)
System.out.println(m.substring(i));
}
}
CHARACTER FUNCTIONS EXAMPLES
import java.util.*;
class SAMPLE1
{
public static void main()
{
char ch='d';
Scanner sc=new Scanner(System.in);
boolean m=Character.isLetter(ch);//true
System.out.println(m);
System.out.println(Character.isLetter('*'));//false
System.out.println(Character.isLetter('0'));// false
System.out.println(Character.isLetter('R'));// true
System.out.println(Character.isDigit('*'));// false
char t='9';
System.out.println(Character.isDigit(t));// true
System.out.println(Character.isLetterOrDigit('R')); // true
System.out.println(Character.isLetterOrDigit('*')); //false
System.out.println(Character.isLetterOrDigit(' ')); // false

System.out.println("________________________________________");
System.out.println(Character.isWhitespace(' '));// true
System.out.println(Character.isWhitespace('\n')); // true
System.out.println(Character.isWhitespace('\t')); //true
System.out.println(Character.isWhitespace(32)); // true
System.out.println(Character.isWhitespace(9)); //true
System.out.println(Character.isWhitespace(10)); //true
System.out.println("_______________________________________");
System.out.println(Character.isUpperCase('R'));// true
System.out.println(Character.isUpperCase('*')); //false
System.out.println(Character.isUpperCase(' ')); //false
System.out.println(Character.isUpperCase(66)); // true
System.out.println(Character.isUpperCase(97)); //false
System.out.println(Character.isLowerCase('p')); //true
System.out.println(Character.isLowerCase('*'));//false
System.out.println(Character.isLowerCase(122));//true

System.out.println("____________________________________________");
System.out.println(Character.toUpperCase('R'));// R
System.out.println(Character.toUpperCase('*')); // *
System.out.println(Character.toUpperCase('t')); // T
System.out.println(Character.toUpperCase(97)); // 65
System.out.println(Character.toUpperCase('P')); //P
System.out.println(Character.toLowerCase('z')); //z
System.out.println(Character.toLowerCase('$'));//$
System.out.println(Character.toLowerCase(90));//122
}
}
PROGRAMS BASED ON WORDS
// CONCAT STARTING LETTER OF EACH WORD
PROGRAM 1:
import java.util.*;
class PROGRAM1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence");
String st=sc.nextLine();
String wd="",str="";
st=st+" ";
for(int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if(ch!=' ')
wd=wd+ch;
else
{
str=str+wd.charAt(0);
wd="";
}
}
System.out.println(str);
}
}
//COUNT THE WORDS THAT BEGIN WITH A OR a
PROGRAM 2:
import java.util.*;
class PROGRAM2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence");
String st=sc.nextLine();
String wd="";int count=0;
st=st+" ";
for(int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if(ch!=' ')
wd=wd+ch;
else
{
if(wd.charAt(0)=='A'||wd.charAt(0)=='a')
count++;
wd="";
}
}
System.out.println(count);
}
}
//COUNT THE WORDS THAT BEGIN WITH ‘S’ AND END WITH ‘S’
PROGRAM 3:
import java.util.*;
class PROGRAM3
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence");
String st=sc.nextLine();
String wd="";int count=0;
st=st+" ";
for(int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if(ch!=' ')
wd=wd+ch;
else
{
if(wd.charAt(0)=='S' && wd.charAt(wd.length()-1)=='S')
count++;
wd="";
}
}
System.out.println(count);
}
}
//PRINT THE WORDS THAT BEGIN WITH A VOWEL
PROGRAM 4:
import java.util.*;
class PROGRAM4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence");
String st=sc.nextLine();
String wd="";int count=0;
st=st+" ";
for(int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if(ch!=' ')
wd=wd+ch;
else
{
ch=wd.charAt(0);
if("AEIOUaeiou".indexOf(ch)!=-1)
System.out.println(wd);
wd="";
}
}
}
}
//PRINT EVEN LENGTH WORDS
PROGRAM 5
import java.util.*;
class PROGRAM5
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence");
String st=sc.nextLine();
String wd="";int count=0;
st=st+" ";
for(int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if(ch!=' ')
wd=wd+ch;
else
{
if(wd.length()%2==0)
System.out.println(wd);
wd="";
}
}
}
}
STRING PATTERN AND NEW PROGRAMS UPDATED
import java.util.*;
class PATTERN1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the string...");
String s=sc.nextLine();
for(int i=0;i<s.length();i++)
System.out.println(s.substring(i));
}
}
enter the string...
SAMPLE

SAMPLE
AMPLE
MPLE
PLE
LE
E
import java.util.*;
class PATTERN2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the string...");
String s=sc.nextLine();
for(int i=s.length();i>=1;i--)
System.out.println(s.substring(0,i));
}
}
enter the string...
COMPUTER
COMPUTER
COMPUTE
COMPUT
COMPU
COMP
COM
CO
C
import java.util.*;
class PATTERN3
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the string...");
String s=sc.nextLine();
for(int i=1;i<=s.length();i++)
System.out.println(s.substring(0,i));
}
}
enter the string...
ICSE
I
IC
ICS
ICSE
import java.util.*;
class PATTERN4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the string...");
String s=sc.nextLine();
for(int i=s.length();i>=2;i--)
System.out.println(s.substring(0,i));
for(int i=1;i<=s.length();i++)
System.out.println(s.substring(0,i));
}
}
enter the string...
PROJECT

PROJECT
PROJEC
PROJE
PROJ
PRO
PR
P
PR
PRO
PROJ
PROJE
PROJEC
PROJECT
import java.util.*;
class PATTERN5
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the string...");
String s=sc.nextLine();
int k=0;
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(s.charAt(k)+" ");
}
System.out.println();
k++;
}
}
}
enter the string...
CAMEL
CCCCC
AAAA
MMM
EE
L

import java.util.*;
class PATTERN6
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the string...");
String s=sc.nextLine();
int k=0;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(s.charAt(k)+" ");
}
System.out.println();
k++;
}
}
}
enter the string...
CAMEL
C
AA
MMM
EEEE
LLLLL
UNIQUE STRING
import java.util.*;
class UNIQUE
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the string...");
String s=sc.nextLine();
int k=0,flag=0;
for(int i=1;i<s.length();i++)
{
char ch=s.charAt(i);
for(int j=i+1;j<s.length();j++)
{
char chr=s.charAt(j);
if(chr==ch)
flag=1;
}
}
if(flag==0)
System.out.println("UNIQUE STRING");
else
System.out.println("NOT UNIQUE STRING");
}
}
enter the string...
computer
UNIQUE STRING

WORD THAT HAS MAXIMUM VOWELS


import java.util.*;
class MAXVOWELS
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence");
String st=sc.nextLine();
String wd="",word="";int c=0;
st=st+" ";
int max=0;
for(int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if(ch!=' ')
{
wd=wd+ch;
if("AEIOUaeiou".indexOf(ch)!=-1)
c++;
}
else
{
if(c>max)
{
max=c;
word=wd;
}
wd="";
c=0;
}
}
System.out.println("The word with maximum vowels "+word);
}
}
Enter the sentence
happy new year
The word with maximum vowels year
LONGEST WORD IN THE SENTENCE
import java.util.*;
class LONGEST
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence");
String st=sc.nextLine();
String wd="",word="";
st=st+" ";
int max=0;
for(int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if(ch!=' ')
wd=wd+ch;
else
{
if(wd.length()>max)
{
max=wd.length();
word=wd;
}
wd="";
}
}
System.out.println("The longest word is::"+word);
}
}
Enter the sentence
we learn java
The longest word is::learn

You might also like