Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Homework HelpRSS Feeds

Program to show an example of getting at characters in a String

Posted By: Daisy Brown     Category: Java     Views: 6937

A Java Program to show an example of getting at characters in a String.

Code for Program to show an example of getting at characters in a String in Java

publicclass JAVA_049
 {


    publicstaticvoid main(String[] args)
    {
       String Text="To be or not to be, that is the question; Whether 'this"
                  +"nobler in the mind to suffer the sling and arrows of "
                  +"outrageous fortune, or to take arms against a sea of "
                  +"troubles, and by opposing end them?";

       System.out.println("Text paragraph is : " + Text + "\n\n");

       int spaces=0;
       int vowels=0;
       int letters=0;

       int text_length=Text.length( );

       char Ch;

       for(int i=0;i<text_length;i++)
       {
          Ch=Text.charAt(i);
          Ch=Character.toLowerCase(Ch);

          if(Ch=='a' || Ch=='e' || Ch=='i' || Ch=='o' || Ch=='u')
             vowels++;

          elseif(Character.isLetter(Ch))
             letters++;

          elseif(Character.isWhitespace(Ch))
             spaces++;
       }

       System.out.println("The Text contained vowels : " + vowels + "\n"
                         +"                   spaces : " + spaces + "\n"
                         +"                  letters : " + letters );
    }
 }
  
Share: 



Daisy Brown
Daisy Brown author of Program to show an example of getting at characters in a String is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!