While learning Java programming I came up with a problem how to count the number of occurrence of capital and small letters in a given sentence or a phrase. In this sample program I use Eclipse IDE for Java that I found very easy to use. Our program will first ask the user to enter a sentence and then our program will count how many time the capital and small letters occurs in a given sentence.
If you have some questions feel free to send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
People here in the Philippines can reach me in this number 09173084360.
Thank you and God Bless.
Sample Program Output
Program Listing
package Capital;
import java.util.Scanner;
public class capital {
public static void main(String[] args)
{
char ch;
int uppercase=0,lowercase=0;
Scanner scan = new Scanner(System.in);
System.out.println("\n");
System.out.print("Count Capital and Small Letters");
System.out.println("\n");
System.out.print("Please Enter a Sentence :=> ");
String str = scan.nextLine();
for(int i=0;i<str.length();i++)
{
ch = str.charAt(i);
int asciivalue = (int)ch;
if(asciivalue >=65 && asciivalue <=90){
uppercase++;
}
else if(asciivalue >=97 && asciivalue <=122){
lowercase++;
}
}
System.out.print("======= DISPLAY RESULT ========");
System.out.print("\n\n");
System.out.println("Number of Lowercase letter(s) :=> " + lowercase + ".");
System.out.println("Number of Uppercase letter(s) :=> " + uppercase + ".");
System.out.print("\n\n");
System.out.print("\t Thank You For Using This Program");
}
}
No comments:
Post a Comment