Converting first character into UpperCase


Write a program to accept a sentence and check whether it is terminated by ‘.’ or ‘? ‘ or ‘!’ . Display the words using default delimiter (space) and the first character of each word should be converted into Upper Case.

Sample Input :

converting first character.

Sample Output:

converting Converting
first First
character Character

import java.util.*;
class strDec
{
public static String firstLetter(String wrd)
    {
        String res="";char ch2=' ';
        int l=wrd.length();
        for(int i=1;i<l;i++)
        {
        char ch1=wrd.charAt(i);
           ch2=wrd.charAt(0);
        if(Character.isLowerCase(ch2))
        {
           ch2 =Character.toUpperCase(ch2);
    
        }
      //System.out.println(ch1);
        res=res +ch1;
        
    }
        res=ch2+res;
        return res;
        //System.out.println(res);
    }
   public static void main(String args[])
  
   {   strDec strd=new strDec();
       String str;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a sentence:");
        str=sc.nextLine();
     int l=str.length();
        char c=str.charAt(l-1);
       
       if(c=='.'||c=='?'||c=='!')
       {
        StringTokenizer st=new StringTokenizer( str, " .?!");
        int n=st.countTokens();
         String wrd[]=new String [n];
       
         String ch[]=new String[n];
         
      
         
        for(int i=0;i<n;i++){
            wrd[i]=st.nextToken();
            ch[i]=firstLetter(wrd[i]);
        System.out.println(wrd[i] + "    "+ch[i]+" " );
        }
        
     }
        
     else
        {
         System.out.println("Enter a sentence terminated with . or ? or ! :");
   }
       
    }
}

Output:

Enter a sentence:
the sky is the limit.
the The
sky Sky
is Is
the The
limit Limit

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();
}

}