String program using StringTokenizer


ISC – Model Question Paper -October 2018

Design a class Chord that accepts a string and prints the number of vowels present in each word of the string. Also note that a string is considered valid if it does not contain any repeated spaces. For example : String : “SUMMER IN AUSTRALIA IS IN DECEMBER.
Output: SUMMER : 2
IN : 1
AUSTRALIA : 5
IS : 1
IN : 1
DECEMBER : 3
Some of the members of the class Chord are given below:
Class Name : Chord
Data members
cd : the string
Member functions:
Chord() : to initialize the member string to null
void fnInput() : to input the member string
void fnOperate() : to print “invalid Sting “ if the string is not valid, otherwise print the words of the member string along with the number of vowels present in them.

boolean isValid() : to verify whether the given string is valid or not and return true or false.
int fnVowel(String w) : to count and return the no. of vowels present in argument ‘w’.

import java.util.*;
class cord
{
String cd;
cord()
{
cd=””;
}
void fnInput()
{
Scanner sc=new Scanner(System.in);
System.out.println(“enter the string:”);
cd=sc.nextLine();
}

//Check the String is valid
boolean isValid()
{
for(int j=0;j<=cd.length()-2;j++)
{
if(cd.charAt(j)==’ ‘ && cd.charAt(j+1)==’ ‘) // checking repeated space
{
return false; //if repeated space, String is invalid
}
}
return true;

}
void fnOperate()
{
fnInput();
if(isValid() == false)
{
System.out.println(“Invalid string”);
return;
}
StringTokenizer st=new StringTokenizer(cd);
int ct=st.countTokens();
for(int j=1;j<=ct;j++)
{
String wrd=st.nextToken();
int cw=fnVowel(wrd);
System.out.println(wrd +” ” +cw);

}
}

//counting the vowels in the argument ‘w’
int fnVowel(String w)
{
int c=0;
w=w.toUpperCase();
for(int j=0; j<=w.length()-1;j++)
{
if(w.charAt(j)==’A’||w.charAt(j)==’E’||w.charAt(j)==’I’||w.charAt(j)==’O’||w.charAt(j)==’U’)
c++;
}
return c;
}
public static void main(String args[])
{
cord c1=new cord();
c1.fnOperate();
}

}

Create a new string by the common characters present in given two strings ( without repetition)


A class Stricom contains two strings and creates a new string using the common characters present in both the strings(without repetition)
For example:     String1  :  “Chinese”       String 2  :   “Checkers”
New string :  “Ches”
Some of the members of the class are given below:
Class Name   : Stricom
Data members:
stA, stB    : the member strings
Member functions:
Stricom(…)     : to initialize  member string to the arguments
String fnCommon()  :  to create a new string using the common characters of both the member strings and return that(use member method fnLen())
void fnShow()           :  to print the member strings and the new string as per the question [ to use the member method fnCommon]
int fnLen(String s)   : to return the length of the argument string.

Specify the class Stricom, and define main() giving the details of the above member data and methods only.

Program:

import java.util.*;
class Stricom
{
String stA, stB;
Stricom(String s1, String s2)
{
stA=s1;
stB=s2;
}
int fnLen(String s)
{
return s.length();
}
void fnShow()
{
String ns=fnCommon();
System.out.println(“string stA :”+ stA +”\nString stB :”+stB);
System.out.print(“New String :”+ns);
}
String fnCommon()
{
//int count=0;
String ns=””, ans=””;
char ch;
for(int j=0;j<=fnLen(stA)-1;j++)
{
for(int k=0;k<=fnLen(stB)-1;k++)
{
if(stA.charAt(j)==stB.charAt(k))
{
// count++;
ns+=stA.charAt(j);
// if(count>=1)
// break;

}
}
}
// return ns;
String ns1=ns;
for(int i=0;i<ns1.length(); i++)
{
ch=ns1.charAt(i);
if(ch!=’ ‘)
{
ans=ans+ch;
ns1=ns1.replace(ch,’ ‘);
}
}
return ans;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the first string:”);
String st1=sc.next();
System.out.println(“Enter the second string:”);
String st2=sc.next();
Stricom sm=new Stricom(st1,st2);
sm.fnShow();
}
}

Output:
Enter the first string:
chinese
Enter the second string:
checkers
string stA :chinese
String stB :checkers
New String :ches