Solved

recognize upper & lower cases

Posted on 2002-05-18
11
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

621 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