How DO you count Upper Case letters?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 22
Reputation: jengels is an unknown quantity at this point 
Solved Threads: 0
jengels jengels is offline Offline
Newbie Poster

How DO you count Upper Case letters?

 
0
  #1
Mar 31st, 2005
I am writing a java program. One of the functions of my program is to count all the uppercase letters in a paragraph of text I import. I am not sure how to set this up.
Is it something like,

isUpperCase.(char)

Any help would be great

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: How DO you count Upper Case letters?

 
0
  #2
Mar 31st, 2005
Maybe something like this would work, but I'm not sure:

  1.  
  2. String text = textArea.getText();
  3. int upperCaseCount = 0;
  4.  
  5. for (int i=0; i<text.length(); i++)
  6. {
  7. for(char c='A'; c<='Z'; c++)
  8. {
  9. if (text.charAt(i) == c)
  10. {
  11. upperCaseCount++;
  12. }
  13. }
  14. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: How DO you count Upper Case letters?

 
0
  #3
Mar 31st, 2005
Here's a real example of it:

  1. class UCase
  2. {
  3. public static void main(String[] args)
  4. {
  5. String s = "This has Three Upper case characters";
  6. int upperCaseCount = 0;
  7.  
  8. for (int i=0; i<s.length(); i++)
  9. {
  10. for(char c='A'; c<='Z'; c++)
  11. {
  12. if (s.charAt(i) == c)
  13. {
  14. upperCaseCount++;
  15. }
  16. }
  17. }
  18. System.out.println(upperCaseCount + "");
  19. }
  20. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: How DO you count Upper Case letters?

 
0
  #4
Apr 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How DO you count Upper Case letters?

 
0
  #5
Apr 1st, 2005
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
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: How DO you count Upper Case letters?

 
0
  #6
Apr 1st, 2005
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
You could use the same thing I gave and still be able to easily extract them and put the UCase characters in a new string.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: How DO you count Upper Case letters?

 
0
  #7
Apr 1st, 2005
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.
Exactly what I did, except I made it much easier.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: How DO you count Upper Case letters?

 
0
  #8
Apr 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: How DO you count Upper Case letters?

 
0
  #9
Apr 1st, 2005
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.
I compare only with uppercase character just as you do. Not meaning to be rude here, but how do you get 5 *2? I mean, don't you have to compare each character with all the ASCII codes pertaining to u case char's?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: How DO you count Upper Case letters?

 
0
  #10
Apr 1st, 2005
Also, how would you do this? would it be something like this:
  1. for (int i=0; i<text.length(); i++)
  2. {
  3. int charPoint = (char)text.charAt(i);
  4. if (charPoint >= 65 && charPoint <= 90)
  5. {
  6. //its ucase
  7. }
  8. }

After writing that it doesn't seem like a bad solution at all.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC