954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How DO you count Upper Case letters?

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

jengels
Newbie Poster
22 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

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

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++;
          }
     }
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Here's a real example of it:

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 + "");
  }
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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.

tonakai
Junior Poster
121 posts since Feb 2005
Reputation Points: 25
Solved Threads: 11
 

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 :)

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 
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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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.

tonakai
Junior Poster
121 posts since Feb 2005
Reputation Points: 25
Solved Threads: 11
 

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?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Also, how would you do this? would it be something like this:

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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

yep, its the solution... :)

tonakai
Junior Poster
121 posts since Feb 2005
Reputation Points: 25
Solved Threads: 11
 
yep, its the solution... :)

I agree, much better ;)

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Or, you could simply check if the character is upper case.

public static void main()
	{
		String s = "This has Three Upper case characters";
		int caps = 0;
		
		for (int i=0; i<s.length(); i++)
		{
			if (Character.isUpperCase(s.charAt(i)))
				caps++;
		}
		
		System.out.println(caps);
		
	}
Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

Or, you could simply check if the character is upper case.

public static void main()
	{
		String s = "This has Three Upper case characters";
		int caps = 0;
		
		for (int i=0; i<s.length(); i++)
		{
			if (Character.isUpperCase(s.charAt(i)))
				caps++;
		}
		
		System.out.println(caps);
		
	}


Even better, good job!

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You