Solved

recognize upper & lower cases

Posted on 2002-05-18
11
308 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

751 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