944,149 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 18368
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 31st, 2005
0

How DO you count Upper Case letters?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jengels is offline Offline
22 posts
since Jan 2005
Mar 31st, 2005
0

Re: How DO you count Upper Case letters?

Maybe something like this would work, but I'm not sure:

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 31st, 2005
0

Re: How DO you count Upper Case letters?

Here's a real example of it:

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 1st, 2005
0

Re: How DO you count Upper Case letters?

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.
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Apr 1st, 2005
0

Re: How DO you count Upper Case letters?

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
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Apr 1st, 2005
0

Re: How DO you count Upper Case letters?

Quote 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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 1st, 2005
0

Re: How DO you count Upper Case letters?

Quote 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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 1st, 2005
0

Re: How DO you count Upper Case letters?

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.
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Apr 1st, 2005
0

Re: How DO you count Upper Case letters?

Quote 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?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 1st, 2005
0

Re: How DO you count Upper Case letters?

Also, how would you do this? would it be something like this:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: in need of a function
Next Thread in Java Forum Timeline: Java Applet Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC