Thursday, October 25, 2012

Displaying First Character Of Each Words Of A Sentence In Upper Case



The program is like: take a sentence and display starting characters in each words in upper case and the rest in lower case. This program is done in two techniques in BlueJ.

Import java.io.*;
class A
{
 String name,str;
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int i,j;
 void take () throws Exception
 {
  System.out.println("Enter any sentence:");
  name=br.readLine();
  }
   void show()
   {
    i=1;
    while(true)
    {
    j=name.indexOf(' ');
    if(j< 0)<0 o:p="o:p">
    break;
    str=name.substring(0,j);
    name=name.substring(j+1);
    for(i=0;i< str.length();i++)
    {
     if(i==0)
      System.out.print(Character.toUpperCase(str.charAt(i)));
      else
       System.out.print(Character.toLowerCase(str.charAt(i)));

    }
    System.out.print(" ");
    }
    for(i=0;i< name.length();i++)
    {
     if(i==0)
      System.out.print(Character.toUpperCase(name.charAt(i)));
      else
       System.out.print(Character.toLowerCase(name.charAt(i)));

    }
    }
    public static void main(String args[])throws Exception
    {
     A ob=new A();
     ob.take();
     ob.show();
     }
     }
     
The same program is done using a different technique.

import java.io.*;
class A
{
 String name;
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int i,j,f=0;
 void take () throws Exception
 {
  System.out.println("Enter any sentence:");
  name=br.readLine();
  }
   void show()
   {
   j=name.length();
   for(i=0;i< j;i++)
   {
    if(i==0)
      System.out.print(Character.toUpperCase(name.charAt(i)));
      else if(name.charAt(i)==' ')
      {
       System.out.print(" "+Character.toUpperCase(name.charAt(i+1)));
       i++;
       }
       else
       System.out.print(Character.toLowerCase(name.charAt(i)));
      }
     }
    public static void main(String args[])throws Exception
    {
     A ob=new A();
     ob.take();
     ob.show();
     }
     }

Related Post: BlueJ Programs on String/Sentence

2 comments:

  1. Sir, can you please upload a specimen paper for java programs likely to come in ISC 2013?

    ReplyDelete
    Replies
    1. After Diwali I will upload expected programs for ISC 2013.

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner