Solved

recognize upper & lower cases

Posted on 2002-05-18
11
307 Views
Last Modified: 2011-09-20
is there a way to count the number of upper and lower case entered in a string..having in mind that the user might enter other than letters..like spaces and other characters..
plus i have to add all the characters in that string..
0
Comment
Question by:ms_lost
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7019076
The Character class has various methods for checking character types, including isLowerCase() and isUpperCase().
0
 

Author Comment

by:ms_lost
ID: 7019172
yeah but how to check that.. since the user will enter a string mix of both
0
 
LVL 92

Assisted Solution

by:objects
objects earned 45 total points
ID: 7019176
Just loop thru the string checking each character:

int lc = 0;
for (int i=0; i<s.length(); i++)
{
  if (Character.isLowerCase(s.charAt(i))) lc++;
}
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 45 total points
ID: 7021786
That will work, although a more performant solution would get the char[] of the String instead of using charAt:

    char[] chars = str.toCharArray();
    for (int i = chars.length - 1; i >= 0; --i) {
        ++numChars;
        if (Character.isLowerCase(chars[i])) {
            ++numLower;
        } else if (Character.isUpperCase(chars[i])) {
            ++numUpper;
        }
    }

Jim
0
 
LVL 92

Expert Comment

by:objects
ID: 7022713
Sorry, I was just focussing on the question and how to count lower case character, and was not convcerned with the performance. And as the string was being entered by the user, and as such would generally be short, then the performance difference would be minimal anyway.
If you really wanted a fast solution that would handle long strings then looping thru the string is not the answer anyway :)
0
 
LVL 92

Expert Comment

by:objects
ID: 7022956
If performance is an issue then the following is a better solution:

char[] c = Arrays.sort((s+"azAZ").toCharArray());
int nlower = Arrays.binarySearch(c, 'z') -
   Arrays.binarySearch(c, 'a') - 1;
int nupper = Arrays.binarySearch(c, 'Z') -
   Arrays.binarySearch(c, 'A') - 1;
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7023978
Hmm. Didn't mean to sound as if I was criticizing -- just extending your remarks. (At least I didn't post as an answer, eh?)

Jim
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 7025506
No offence taken :-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 8766847
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- split points between objects@idg and jim_cakalic@idg

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 8766874
Friday is _cleanup_ day! OK by me. :-)
Jim
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 106
Configure a Bean in an XML file 4 42
Java string replace 11 55
What's wrong with this code? 4 26
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question