i need help with these problem.. just new in world of programming...
Write a program that allows you to input a string. Your program will count and extract and display the number of upper case letters and lower case letters.
Sample output:
Enter a string: Hello World
Upper case letters: 2 - HW
Lower case letters: 8 – elloorld
java programming: count upper case and lower case letter
Started by jun71178, Oct 26 2010 06:56 AM
5 replies to this topic
#1
Posted 26 October 2010 - 06:56 AM
|
|
|
#2
Posted 26 October 2010 - 09:48 AM
Here is a piece of code that would do that;
public class Test {
public static void main(String[] args) {
String s = "Hello World";
String upper ="";
String lower = "";
String other = "";
for (int i = 0; i < s.length(); i++) {
char thisChar = s.charAt(i);
if (thisChar >= 65 && thisChar <= 90) {
upper += thisChar;
} else if (thisChar >= 97 && thisChar <= 122) {
lower += thisChar;
} else {
other += thisChar;
}
}
System.out.println("Upper case letters: " + upper.length() + " - " + upper);
System.out.println("Lower case letters: " + lower.length() + " - " + lower);
System.out.println("Other characters: " + other.length() + " - " + other);
}
}
Edited by Metalhead, 26 October 2010 - 09:49 AM.
Translated Dutch word to English
#3
Posted 26 October 2010 - 05:43 PM
Thanks a lot to you master Metalhead!!! it gives me an idea on how to start.... i will just add BufferedReader to get the input to be count...........More power to you master!!!
#4
Posted 26 October 2010 - 06:50 PM
here is what i did to count the input lower case and upper case letter from user....... thanks to master Metalhead for his code....
here is the sample output:
Enter a String: My Age is 32
Upper case letters: 2 - MA
Lower case letters: 5 - ygeis
Other: 32
Process completed.
and here is my edited code thanks again to master Metalhead:
import java.io.*;
public class casestudy4 {
public static void main(String[] args)throws IOException {
BufferedReader enterstring=new BufferedReader(new InputStreamReader(System.in));
String inputstring;
String upper ="";
String lower ="";
String other ="";
System.out.print("Enter a String: ");
inputstring=enterstring.readLine();
for (int i = 0; i < inputstring.length(); i++) {
char thisChar = inputstring.charAt(i);
if (thisChar >= 65 && thisChar <= 90) {
upper += thisChar;
} else if (thisChar >= 97 && thisChar <= 122) {
lower += thisChar;
} else {
other += thisChar;
}
}
System.out.println("Upper case letters: " + upper.length() + " - " + upper);
System.out.println("Lower case letters: " + lower.length() + " - " + lower);
System.out.println("Other: "+other);
}
}
it may serve as reference for newbie programmer like us..........
here is the sample output:
Enter a String: My Age is 32
Upper case letters: 2 - MA
Lower case letters: 5 - ygeis
Other: 32
Process completed.
and here is my edited code thanks again to master Metalhead:
import java.io.*;
public class casestudy4 {
public static void main(String[] args)throws IOException {
BufferedReader enterstring=new BufferedReader(new InputStreamReader(System.in));
String inputstring;
String upper ="";
String lower ="";
String other ="";
System.out.print("Enter a String: ");
inputstring=enterstring.readLine();
for (int i = 0; i < inputstring.length(); i++) {
char thisChar = inputstring.charAt(i);
if (thisChar >= 65 && thisChar <= 90) {
upper += thisChar;
} else if (thisChar >= 97 && thisChar <= 122) {
lower += thisChar;
} else {
other += thisChar;
}
}
System.out.println("Upper case letters: " + upper.length() + " - " + upper);
System.out.println("Lower case letters: " + lower.length() + " - " + lower);
System.out.println("Other: "+other);
}
}
it may serve as reference for newbie programmer like us..........
#5
Posted 26 October 2010 - 09:06 PM
If you expect long Strings and you might want to use a StringBuilder for the String-concatenation;
StringBuilder upper = new StringBuilder();
and then
upper.append(thisChar);
etc...
It is a lot quicker than using 'upper += thisChar'...
StringBuilder upper = new StringBuilder();
and then
upper.append(thisChar);
etc...
It is a lot quicker than using 'upper += thisChar'...
#6
Posted 26 October 2010 - 11:25 PM
thanks master Metalhead.......... i really appreciate your attention....... hope you extend your helping thoughts to those needy newbie like me.....
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









