[Go to site: main page, start]

0% found this document useful (0 votes)
3 views7 pages

Basic Programs Using String Tokenizer

The document contains multiple Java programs that utilize the StringTokenizer class to perform various string manipulations. These programs include counting words that start with specific letters, finding the longest and shortest words, converting words to Pig Latin, and analyzing the frequency of vowels and consonants. Each program prompts the user for input and processes the string accordingly, demonstrating different functionalities of string handling in Java.

Uploaded by

Sarvesh Kumar
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)
3 views7 pages

Basic Programs Using String Tokenizer

The document contains multiple Java programs that utilize the StringTokenizer class to perform various string manipulations. These programs include counting words that start with specific letters, finding the longest and shortest words, converting words to Pig Latin, and analyzing the frequency of vowels and consonants. Each program prompts the user for input and processes the string accordingly, demonstrating different functionalities of string handling in Java.

Uploaded by

Sarvesh Kumar
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

PROGRAMS USING STRING TOKENIZER

// WORDS THAT BEGIN AND END WITH A VOWEL


PROGRAM 1:
import [Link].*;
class COUNT2
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the string....");
String s=[Link]();// we learn java
StringTokenizer str=new StringTokenizer(s,” ! ? . , # “);
while([Link]())
{
String wd=[Link]();
char ch1=[Link](0);
char ch2=[Link]([Link]()-1);
if(("AEIOUaeiou".indexOf(ch1)!=-1) && ("AEIOUaeiou".indexOf(ch2)!=-1))
[Link](wd);
}
}
}
PROGRAM 2:
//WORDS THAT BEGIN WITH A OR a
import [Link].*;
class COUNT
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the string....");
String s=[Link]();
int c=0;
StringTokenizer str=new StringTokenizer(s);
while([Link]())
{
String wd=[Link]();
if([Link](0)=='A'||[Link](0)=='a')
c++;
}
[Link]("Total number of words that begin with 'a' is ..."+c);
}
}
LONGEST WORD SHORTEST WORD
import [Link].*;
class COUNT
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the string:");
String s=[Link]();
StringTokenizer str=new StringTokenizer(s);
String wd=[Link](); // first word
String longest=wd;
String shortest=wd;
while([Link]())
{
wd=[Link]();
if([Link]()>[Link]())
longest=wd;
if([Link]()<[Link]())
shortest=wd;
}
[Link]("Longest word:"+longest);
[Link]("Shortest word:"+shortest);
}
}
PROGRAM 3:
//PRINTING SAME LETTER PAIR WORD WISE
import [Link].*;
class COUNT7
{
public static void main() // rabbit eats carrots letter
{
Scanner sc=new Scanner([Link]);
[Link]("enter the paragraph....");
String s=[Link]();
char c=[Link]([Link]()-1);
if(!(c=='.'||c=='?'||c=='!'))
{
[Link]("Invalid string");
[Link](0);
}
StringTokenizer str=new StringTokenizer(s, " , ! ? . ");
while([Link]())
{
String wd=[Link]();
int f=0;
String pairs="";
for(int i=0;i<[Link]()-1;i++) //rabbit
{
char ch1=[Link](i);
char ch2=[Link](i+1);
if(ch1==ch2)
{
pairs=pairs+” “+ch1+ch2;
f=1;
}
}
if(f==1)
[Link](wd+" "+pairs);
} // end of while

}
}
PROGRAM 4:
// FREQUENCY OF VOWEL ‘V’ CONSONANTS ‘C’
import [Link].*;
class COUNT5
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the string....");
String s=[Link]();
char c=[Link]([Link]()-1);
if(!(c=='.'||c=='?'||c=='!'))
{
[Link]("Invalid string");
[Link](0);
}
StringTokenizer str=new StringTokenizer(s," , ! ? . ");
while([Link]())
{
String wd=[Link]();
int i;
[Link]();
[Link](wd+"\t");
for(i=0;i<[Link]();i++)
{
char ch=[Link](i);
if("AEIOUaeiou".indexOf(ch)!=-1)
[Link]("V"+" ");
}
[Link]();
[Link]("\t");
for(i=0;i<[Link]();i++)
{
char ch=[Link](i);
if("AEIOUaeiou".indexOf(ch)==-1)
[Link]("C"+" ");
}
}
}
}
PROGRAM 5:
//CONVERT EACH WORD INTO ITS PIGLATIN FORM
import [Link].*;
class COUNT4
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the string....");
String s=[Link]();
StringTokenizer str=new StringTokenizer(s," , ! ? . ");
while([Link]())
{
String wd=[Link]();
int i;
for(i=0;i<[Link]();i++)
{
char ch=[Link](i);
if("AEIOUaeiou".indexOf(ch)!=-1)
break;
}
String a=[Link](i);
String b=[Link](0,i);
[Link](a+b+"ay"+" ");
}
}
}
PROGRAM 6:
// WORD WISE REPORT OF NUMBER OF VOWELS & CONSONANTS
import [Link].*;
class COUNT3
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the string....");
String s=[Link]();
StringTokenizer str=new StringTokenizer(s," , ! ? . ");
while([Link]())
{
String wd=[Link]();
int v=0,c=0;
for(int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if("AEIOUaeiou".indexOf(ch)!=-1)
v++;
else
c++;
}
[Link](wd+" "+v+" "+c);
}

}
}
PROGRAM 7:
//FREQUENCY OF A WORD IN A PARAGRAPH
import [Link].*;
class COUNT6
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the paragraph....");
String s=[Link]();
[Link]("enter the word....");
String word=[Link]();
int count=0;
char c=[Link]([Link]()-1);
if(!(c=='.'||c=='?'||c=='!'))
{
[Link]("Invalid string");
[Link](0);
}
StringTokenizer str=new StringTokenizer(s," , ! ? . ");
while([Link]())
{
String wd=[Link]();
if([Link](word))
count++;
}
[Link]("Frequency of the word present...."+count);
}
}
PROGRAM 8:
// CAPITALISE EACH WORD AND VOWELS, CONSONANTS PRESENT IN
EACH WORD
import [Link].*;
class COUNTVOWELS
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the string....");
String s=[Link]();
char c=[Link]([Link]()-1);
if(!(c=='.'||c=='?'))
{
[Link]("Invalid string");
[Link](0);
}
StringTokenizer str=new StringTokenizer(s," , ! ? . ");
String st="";
while([Link]())
{
String wd=[Link]();
st=st+[Link]([Link](0))+[Link](1)+" ";
int vow=0,cons=0;
for(int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if("AEIOUaeiou".indexOf(ch)!=-1)
vow++;
else
cons++;
}
[Link](wd+" "+vow+" "+cons);
}
[Link](st);
}
}

You might also like