Solved

recognize upper & lower cases

Posted on 2002-05-18
11
306 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

777 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