|
 |
|
 |
10-13-2004, 04:42 PM
|
#1 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
Upper case string to lower case
Hey guys... Kinda knew to this board... I'm a super moderator over at MSFN.org which is a big Windows-related board...
Anywayz, I'm having some trouble converting a string that has upper case letters and lower case letters to a string that only has lower case letters...
I've tried using the tolower() function, but Dev-C++ keeps saying that it does not exist (it probably doesn't)...
Anywayz, is there a way to do it either in my main program with an existing function or as a new function within the program?
|
|
|
10-13-2004, 04:43 PM
|
#2 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
Re: Upper case string to lower case
Quote:
Originally posted by gamehead200
which is a big Windows-related board...
|
pfff, windows. i hate windows.
|
|
|
10-13-2004, 06:22 PM
|
#3 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
No need to hate  . We can do it nicely OS-independant:
Code:
#include <iostream>
#include <cctype>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "UPPERCASE (?)";
transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
cout<<s<<endl;
return 0;
}
__________________
|
|
|
10-13-2004, 06:42 PM
|
#4 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
looks complicated. we never learned that in class.
Code:
#include <algorithm>
we never even touched this.
|
|
|
10-13-2004, 10:24 PM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
|
Then do this:
Code:
/* this is the part which practicaly does, what
* tolower() would do. It also take abitrary chars like
* Æ -> æ, Ø -> ø, Å -> å, Ä -> ä, Ü -> ü, Ö -> ö, etc.
*/
int to_lower(int n)
{
/* shift what ever is between A - Z */
if(n >= 'A' && n <= 'Z')
return (n + ('a' - 'A'));
/* catch all abitrary chars
* perhaps I've forgot some,
* but most of them are here
*/
switch(n)
{
case 'Æ':
return 'æ';
case 'Ø':
return 'ø';
case 'Å':
return 'å';
case 'Ë':
return 'ë';
case 'Ê':
return 'ê';
case 'Ñ':
return 'ñ';
case 'Ü':
return 'ü';
case 'Û':
return 'û';
case 'Ï':
return 'ï';
case 'Î':
return 'î';
case 'Ö':
return 'ö';
case 'Ô':
return 'ô';
case 'Õ':
return 'õ';
case 'Ä':
return 'ä';
case 'Â':
return 'â';
case 'Ã':
return 'ã';
default:
/* if none of the above,
* it's a lower or unconvertable char
*/
return n;
}
}
|
|
|
10-14-2004, 05:10 AM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
Originally posted by Androto
looks complicated. we never learned that in class.
Code:
#include <algorithm>
we never even touched this.
|
It is not difficult at all. Use it as you see it. This is modern C++. I could re-invent the wheel though, but STL (Standard Template Library) offers many containers and algorithms that prevents us from inventing code that is going to be used over and over again. In the end, you'll want the job to get done.
Lets re-invent the wheel. Whatever extended ascii tables exist out there, I'll use only the official ascii table. See below in my reply to redhead:
redhead
Let's play with math a lil shall we :)
I'll complement your code for std::string. See way below for last little step for Androto:
Code:
string to_lowercase(const string& s)
{
string sResult;
for(unsigned int i = 0; i < s.size(); ++i)
{
if((s[i] >= 'A') && (s[i] <= 'Z'))
sResult += (s[i] | 32);
else sResult += s[i];
}
return sResult;
}
back to gamehead
Here is the complete code for you to play with (just copy and paste):
Code:
#include <iostream>
#include <string>
using namespace std;
//Optional functions: depends on IDE.
void wait_for_enter();
void reset_istream();
//Core components.
string to_lowercase(const string& s);
string to_uppercase(const string& s);
int main(int argc, char *argv[])
{
string theString("- HI ALL !?% hi all =");
//Let's try the both of them :)
theString=to_lowercase(theString);
cout<<theString<<endl;
theString=to_uppercase(theString);
cout<<theString<<endl;
wait_for_enter();
return 0;
}
////////////////////////////////////////////////
void wait_for_enter()
{
cout << "press <enter> to continue...";
// Reset failstate, just s case.
cin.clear();
string line;
getline( cin, line);
}
////////////////////////////////////////////////
void reset_istream( )
{
char ch;
// Reset the state.
cin.clear();
// Remove all characters until we find a newline or EOF.
ch = cin.get();
while ((ch != '\n')&&(ch != EOF))
ch = cin.get();
cin.clear();
}
//////////////////////////////////////////////////
string to_lowercase(const string& s)
{
string sResult;
for(unsigned int i = 0; i < s.size(); ++i)
{
if((s[i] >= 'A') && (s[i] <= 'Z'))
sResult += (s[i] | 32);
else sResult += s[i];
}
return sResult;
}
////////////////////////////////////////////////////
string to_uppercase(const string& s)
{
string sResult;
for(unsigned int i = 0; i < s.size(); ++i)
{
if((s[i] >= 'a') && (s[i] <='z'))
sResult += (s[i] & (~32));
else sResult += s[i];
}
return sResult;
}
////////////////////////////////////////////////////
gamehead, your task
I haven't covered the non-official characters. Pick an optional ascii table ( www.google.com) and try to implement redhead's idea with my code for std::string. These were redhead's optional haracters:
Code:
switch(n)
{
case 'Æ':
return 'æ';
case 'Ø':
return 'ø';
case 'Å':
return 'å';
case 'Ë':
return 'ë';
case 'Ê':
return 'ê';
case 'Ñ':
return 'ñ';
case 'Ü':
return 'ü';
case 'Û':
return 'û';
case 'Ï':
return 'ï';
case 'Î':
return 'î';
case 'Ö':
return 'ö';
case 'Ô':
return 'ô';
case 'Õ':
return 'õ';
case 'Ä':
return 'ä';
case 'Â':
return 'â';
case 'Ã':
return 'ã';
default:
/* if none of the above,
* it's a lower or unconvertable char
*/
return n;
}
Let's only try the "basic printable" characters only. Try it the simple direct way first (switch). Then try it with math. You'll learn some old fashioned coding alright :).
Start first by finding a non-official (extended) ascii table on the net, so we can have a reference as well.
__________________
Last edited by Valmont; 10-14-2004 at 12:53 PM.
|
|
|
10-14-2004, 12:38 PM
|
#7 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
i'm still stuck trying to figure out how to do the other program. the one where the person who is running the program types in a couple of words, then the words get repeated. (this part i could do). Next, the words get analyzed. eg: if i typed in "help me" it would say there are 7 characters, 2 e's, 1 h, 1 l, 1 m, and one p. that part i can't figure out. i have no idea as to how arrays actually work. but i'm hoping that the more i hang around them the more i'll understand them.
|
|
|
10-14-2004, 12:52 PM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
I meant gamehead, not you. Sorry.
I've corrected my previous post.
__________________
|
|
|
10-14-2004, 02:36 PM
|
#9 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
Hey... Thanks guys! I really appreciate... I'll probably be able to use this in the future, since it was really optional for this assignment, but then I realized it was a bit more complicated then I thought!  Although, Valmont's first post looks a bit easier... I hope my teacher won't mind me putting that in! 
|
|
|
10-14-2004, 02:37 PM
|
#10 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
Quote:
Originally posted by Androto
i'm still stuck trying to figure out how to do the other program. the one where the person who is running the program types in a couple of words, then the words get repeated. (this part i could do). Next, the words get analyzed. eg: if i typed in "help me" it would say there are 7 characters, 2 e's, 1 h, 1 l, 1 m, and one p. that part i can't figure out. i have no idea as to how arrays actually work. but i'm hoping that the more i hang around them the more i'll understand them.
|
It says there's 7 characters because it counts the space as a character... 
|
|
|
10-14-2004, 03:00 PM
|
#11 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
correct!
|
|
|
10-14-2004, 11:06 PM
|
#12 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
|
Quote:
Orriginaly posted by Valmont
redhead
Let's play with math a lil shall we
|
I know about the math in it, I just wanted to show that tolower() actualy shift printable chars the value the current ASCII table has between lower and upper charactor.
It's easier to get a grasp of it, with chars representing it, than just some integer.
|
|
|
10-15-2004, 03:24 AM
|
#13 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
Originally posted by redhead
I know about the math in it,...
|
I know you know, I just thought you'd like to see that one.
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 06:36 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|