Solved

recognize upper & lower cases

Posted on 2002-05-18
11
304 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changePi Challenge 15 76
noX challenge 17 76
where is session ID cache stored 1 39
Core Java. What output will be and why ? 6 58
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now