Jump to content

java programming: count upper case and lower case letter

- - - - -

  • Please log in to reply
5 replies to this topic

#1
jun71178

jun71178

    Newbie

  • Members
  • PipPip
  • 17 posts
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

#2
Metalhead

Metalhead

    Newbie

  • Members
  • PipPip
  • 27 posts
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
jun71178

jun71178

    Newbie

  • Members
  • PipPip
  • 17 posts
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
jun71178

jun71178

    Newbie

  • Members
  • PipPip
  • 17 posts
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..........

#5
Metalhead

Metalhead

    Newbie

  • Members
  • PipPip
  • 27 posts
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'...

#6
jun71178

jun71178

    Newbie

  • Members
  • PipPip
  • 17 posts
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