| | |
How DO you count Upper Case letters?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Maybe something like this would work, but I'm not sure:
Java Syntax (Toggle Plain Text)
String text = textArea.getText(); int upperCaseCount = 0; for (int i=0; i<text.length(); i++) { for(char c='A'; c<='Z'; c++) { if (text.charAt(i) == c) { upperCaseCount++; } } }
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Here's a real example of it:
Java Syntax (Toggle Plain Text)
class UCase { public static void main(String[] args) { String s = "This has Three Upper case characters"; int upperCaseCount = 0; for (int i=0; i<s.length(); i++) { for(char c='A'; c<='Z'; c++) { if (s.charAt(i) == c) { upperCaseCount++; } } } System.out.println(upperCaseCount + ""); } }
I'd expect a regular expression that extracts all uppercase letters into a new String, followed by simply taking the length of that String, to be the shortest code.
The regular expression could get messy though, and not being an expert at them I'm not going to try writing one
The regular expression could get messy though, and not being an expert at them I'm not going to try writing one
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
•
•
•
•
Originally Posted by jwenting
I'd expect a regular expression that extracts all uppercase letters into a new String, followed by simply taking the length of that String, to be the shortest code.
The regular expression could get messy though, and not being an expert at them I'm not going to try writing one
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
•
•
•
•
Originally Posted by tonakai
you can look at the values of the character, (for ASCII code table)
for example value of 'A' is 65 and value of 'Z' is 90
so you can check every character in your string, then check if characters are between 65 and 90, if true then increase counter, if false do nothing.
well your code is a bit slower, because in every character your program compare with the whole alphabet, but mine only with two number,
so in a string with 5 character,
yours, 5*26 comparison,
mine, only 5*2
accually when string is very long, both algorithm's time gets closer... both algorithm's speed is O(n) where n is the length of the string.
so in a string with 5 character,
yours, 5*26 comparison,
mine, only 5*2
accually when string is very long, both algorithm's time gets closer... both algorithm's speed is O(n) where n is the length of the string.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
•
•
•
•
Originally Posted by tonakai
well your code is a bit slower, because in every character your program compare with the whole alphabet, but mine only with two number,
so in a string with 5 character,
yours, 5*26 comparison,
mine, only 5*2
accually when string is very long, both algorithm's time gets closer... both algorithm's speed is O(n) where n is the length of the string.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Also, how would you do this? would it be something like this:
After writing that it doesn't seem like a bad solution at all.
Java Syntax (Toggle Plain Text)
for (int i=0; i<text.length(); i++) { int charPoint = (char)text.charAt(i); if (charPoint >= 65 && charPoint <= 90) { //its ucase } }
After writing that it doesn't seem like a bad solution at all.
![]() |
Similar Threads
- Upper Case Letters (Python)
Other Threads in the Java Forum
- Previous Thread: in need of a function
- Next Thread: Java Applet Help
| Thread Tools | Search this Thread |
actuate android api applet application array arrays automation balls binary bluetooth bold business c++ chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging draw ebook eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui hql html ide image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie nextline numbers oracle parameter php print problem program programming project recursion recursive scanner screen server set size sms sort sql string sun swing swt threads time tree user websites windows






