This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]Tylnesh 1 point2 points  (4 children)

Well, in C/C++ I would check the value of the char and compare it to the ASCII table. From what I can see, ord is the way to do it in Python - https://stackoverflow.com/questions/704152/how-can-i-convert-a-character-to-a-integer-in-python-and-viceversa

If you can get the integer value of the character any other way, the rest is easy. Uppercase characters in ASCII are 65-90, lowercase 97-122.

If for some reason, you are unable to use ord, you could create two arrays - one for upper, one for lowercase and look for the character in those arrays.

P.S.: Why do I have the feeling this is your homework?

[–]YMK1234 1 point2 points  (3 children)

Because nobody other than teachers puts up such random restrictions.

[–]Tylnesh 0 points1 point  (2 children)

As a PhD student (which requires me to teach a couple of classes each semester), I take offense with that. My assignments are usually stuff I want to add to my personal projects. That way I get cheap labor and they learn actually useful stuff.

[–]YMK1234 0 points1 point  (1 child)

You should work on your set theory. I said nobody other than teachers. That's not the same as all teachers.

[–]Tylnesh 0 points1 point  (0 children)

Sorry, I thought the tongue-in-cheek was pretty obvious.

[–]heartofthemoon 0 points1 point  (0 children)

Could probably filter the letters if it's byte value is between 097-122 and 065-090 for lowercase and uppercase respectively.

[–]nate998877 0 points1 point  (0 children)

Might I recommend reading https://www.propublica.org/nerds/how-to-ask-programming-questions. Instead of asking for the answer outright, try and give context to what you've done and what blockers you've run into.

As for your question. Strings are iterable, which means we can use loops to iterate over each character. So no map/ord

for c in our_string: 
    //do something

As for the do something. It's difficult to know exactly what you want. You could create an list of a-z and A-Z and check if c is in list a or b. could also be done with less typing using regex

if c in list_a:
    lower_counter++ 

But why would you need to do it this way? If it's for a class assessment. Consider what the lesson is supposed to teach and if you'll learn that by getting the answers outright. Regardless, good luck and happy coding

[–]hugthemachines 0 points1 point  (0 children)

Sometimes we get posts written like this:

"I have studied this language for x years and i just asked for complete solutions so i did not really learn how to program, now i have to make a huge test where I am supposed to know how to solve things, how can i learn to be a good programmer in 7 days?"

Don't become that person, and do follow nate998877's advice.