Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 6th, 2009, 12:10 PM   #1
csaawariya
Newbie
 
Join Date: Jan 2009
Posts: 1
Rep Power: 0 csaawariya is on a distinguished road
how to convert any string from upper case to lower case

hi can you please help me with this..
Without using any standard library functions, how to write a function that accepts a string and toggles all of the individual
letters within between upper and lower case – optimally.
For example, if the input string were “Mixed Case!” the output
would be “mIXED cASE!”
thanks in advance...
csaawariya is offline   Reply With Quote
Old Jan 6th, 2009, 12:15 PM   #2
Arla
Expert Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 777
Rep Power: 10 Arla is on a distinguished road
Re: how to convert any string from upper case to lower case

Assuming this is homework, I'll give you a hint..

Check the ascii value for each character, if it's in the range used by uppercase characters, convert to the equivalent lower-case letter.

(Hint: Upper Case A is 65, lower case is 97)
Arla is offline   Reply With Quote
Old Jan 6th, 2009, 12:24 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Google / Kitchener
Posts: 4,154
Rep Power: 15 Sane has a spectacular aura aboutSane has a spectacular aura about
Send a message via MSN to Sane
Re: how to convert any string from upper case to lower case

You don't even need to know the ASCII values. Characters are implicitly represented by integers in C/C++:

if(c >= 'a' && c <= 'z') {
    // Do something with the lower case letter
}
...

Wrap a series of if conditions like that in a for loop, and the rest should be easy if you've been doing your homework.
__________________
PFO's Folding@Home Team | Sane's Monthly Algorithms Challenges
Rules | How to Post a Question | How to Post Code

Becoming a good programmer requires foresight of your code's execution.
Becoming an excellent programmer requires foresight of your code's modification.
Sane is offline   Reply With Quote
Old Jan 6th, 2009, 3:42 PM   #4
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,676
Rep Power: 11 lectricpharaoh will become famous soon enough
Re: how to convert any string from upper case to lower case

Not to denigrate the two posters above me, but I'd recommend using C's character testing/conversion functions. The reason is that not every system uses ASCII, and there is no guarantee in C or C++ that a given set of values will be contiguous (or even in the order you expect). EBCDIC is a good example, and while it's not likely to be used on the OP's computer, what about Unicode? What about languages besides English? Is that weird accented character considered uppercase or lowercase, or even a letter, when it doesn't fall within the prescribed range? Assuming you can do a test to see if a value is >= 'A' and <= 'Z', and then processing it accordingly, will not always give the desired result. Likewise, adding or subtracting a fixed value to convert between uppercase and lowercase won't always work as intended, either.

@csaawariya: I would instead recommend using either isupper() or islower() to test the case; you can then call toupper() or tolower() as appropriate to change the case. Remember to #include <cctype>, as this header is where the functions are prototyped. You can read about the functions here. Your programming task is very trivial, and I'm not going to do your homework for you; I've already given you everything you need. If you have further troubles, post the code you've written when asking for help. Otherwise, you will just appear lazy.
__________________
My microwave has settings for snake, gremlin, and puppy.
lectricpharaoh is offline   Reply With Quote
Old Jan 6th, 2009, 3:48 PM   #5
Arla
Expert Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 777
Rep Power: 10 Arla is on a distinguished road
Re: how to convert any string from upper case to lower case

I guess I took this "Without using any standard library functions" to mean you couldn't use .toUpper() or .toLower() type of functions.
__________________
I can remember, back in '22
They changed the law - came knocking on the door
In that same moment, the broadband seemed to go..
Phones all dead. Gone dizzy in the head..
Arla is offline   Reply With Quote
Old Jan 6th, 2009, 3:50 PM   #6
Arla
Expert Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 777
Rep Power: 10 Arla is on a distinguished road
Re: how to convert any string from upper case to lower case

An alternative approach, if you want to go the REALLY long route, would just be to check each character with a massive switch statement (or case statement, or whatever they are called in C++) and have it build the string up, with a case for each of the upper case characters, and then a default to move anything else straight from input to output.
__________________
I can remember, back in '22
They changed the law - came knocking on the door
In that same moment, the broadband seemed to go..
Phones all dead. Gone dizzy in the head..
Arla is offline   Reply With Quote
Old Jan 6th, 2009, 4:08 PM   #7
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,676
Rep Power: 11 lectricpharaoh will become famous soon enough
Re: how to convert any string from upper case to lower case

Quote:
Originally Posted by Arla
I guess I took this "Without using any standard library functions" to mean you couldn't use .toUpper() or .toLower() type of functions.
Oops, my bad. Well then, if I were him, I'd ask the professor what character representation system is in use, as this is necessary information to code a solution.
__________________
My microwave has settings for snake, gremlin, and puppy.
lectricpharaoh is offline   Reply With Quote
Old Jan 6th, 2009, 5:10 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Google / Kitchener
Posts: 4,154
Rep Power: 15 Sane has a spectacular aura aboutSane has a spectacular aura about
Send a message via MSN to Sane
Re: how to convert any string from upper case to lower case

Quote:
Originally Posted by lectricpharaoh View Post
Oops, my bad. Well then, if I were him, I'd ask the professor what character representation system is in use, as this is necessary information to code a solution.
There's no need to be smart to the teacher. If this is a standard homework assignment, it goes without saying that he should do as Arla and I said. I know you're trying to preach good practice, but come on, that's a bit excessive for such a simple grade 10 exercise. And I'm even the one who usually challenges the instructor with these kinds of questions.
__________________
PFO's Folding@Home Team | Sane's Monthly Algorithms Challenges
Rules | How to Post a Question | How to Post Code

Becoming a good programmer requires foresight of your code's execution.
Becoming an excellent programmer requires foresight of your code's modification.
Sane is offline   Reply With Quote
Old Jan 6th, 2009, 6:01 PM   #9
Arla
Expert Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 777
Rep Power: 10 Arla is on a distinguished road
Re: how to convert any string from upper case to lower case

Quote:
Originally Posted by lectricpharaoh View Post
Oops, my bad. Well then, if I were him, I'd ask the professor what character representation system is in use, as this is necessary information to code a solution.
Not really, my other solution (case/switch statement) wouldn't care what representation was used.

It would also require the person asking the question know what is mean't, based on this assignment my guess is that the person posting doesn't know what different character representations exist, but I could be being really mean and unfair
__________________
I can remember, back in '22
They changed the law - came knocking on the door
In that same moment, the broadband seemed to go..
Phones all dead. Gone dizzy in the head..
Arla is offline   Reply With Quote
Old Jan 6th, 2009, 6:12 PM   #10
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,676
Rep Power: 11 lectricpharaoh will become famous soon enough
Re: how to convert any string from upper case to lower case

Quote:
Originally Posted by Sane
There's no need to be smart to the teacher.
That's not what I'm saying at all. Whether this is in high school or college, one of the responsibilities of the teacher is to clarify any ambiguities in assignments.

Now, it's entirely possible that the teacher's original assignment wording is not ambiguous, but going only by the OP's post, even I have a couple of questions.

For example, regarding the prohibition on standard library functions, does this mean no function to convert the string entirely, or no standard library functions at all? Imagine the task was to sort an array; the instructor might say not to use library functions for the sort, intending to bar the use of std::qsort(), std::sort() and the like, while not intending to bar std::swap(). In the OP's case, the intent might be to teach him how to iterate through an array and process each element in turn, so perhaps isupper(), etc might be okay- again, the only way to know is to ask. Certainly, I don't think the instructor intended a global barring of all standard library functions; output for one would be pretty hard to achieve in this case.
Quote:
Originally Posted by Sane
If this is a standard homework assignment, it goes without saying that he should do as Arla and I said. I know you're trying to preach good practice, but come on, that's a bit excessive for such a simple grade 10 exercise.
I realize I sometimes come across as pedantic, but again, that's not my intent. Do you expect that many grade 10 students could write this program, including outputting the result, without using any standard library functions at all?
__________________
My microwave has settings for snake, gremlin, and puppy.
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a word to lower case 357mag C++ 6 Jun 7th, 2007 12:52 PM
Convert String to Int Cool1Net6 C++ 15 Dec 10th, 2006 3:48 PM
convert string to int lucifer C# 21 Dec 1st, 2005 9:47 PM
Convert a string to unicode string sma_soft Delphi 3 Nov 16th, 2005 5:06 AM
Convert int to string Programmingb52 C# 1 Apr 23rd, 2005 5:16 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC