Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-13-2004, 04:42 PM   #1 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
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?
gamehead200 is offline   Reply With Quote
Old 10-13-2004, 04:43 PM   #2 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
Re: Upper case string to lower case

Quote:
Originally posted by gamehead200
which is a big Windows-related board...
pfff, windows. i hate windows.
Androto is offline   Reply With Quote
Old 10-13-2004, 06:22 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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;
}
__________________
Valmont is offline   Reply With Quote
Old 10-13-2004, 06:42 PM   #4 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
looks complicated. we never learned that in class.

Code:
#include <algorithm>
we never even touched this.
Androto is offline   Reply With Quote
Old 10-13-2004, 10:24 PM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
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;
  }
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-14-2004, 05:10 AM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
Valmont is offline   Reply With Quote
Old 10-14-2004, 12:38 PM   #7 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
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.
Androto is offline   Reply With Quote
Old 10-14-2004, 12:52 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I meant gamehead, not you. Sorry.
I've corrected my previous post.
__________________
Valmont is offline   Reply With Quote
Old 10-14-2004, 02:36 PM   #9 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
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!
gamehead200 is offline   Reply With Quote
Old 10-14-2004, 02:37 PM   #10 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
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...
gamehead200 is offline   Reply With Quote
Old 10-14-2004, 03:00 PM   #11 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
correct!
Androto is offline   Reply With Quote
Old 10-14-2004, 11:06 PM   #12 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-15-2004, 03:24 AM   #13 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help for another program Androto Standard C, C++ 54 10-15-2004 07:21 AM
From C to Java HighterDK Java 11 07-13-2004 07:15 PM
First Letter Upper Case sde Java 7 05-03-2004 07:58 AM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM


All times are GMT -8. The time now is 06:36 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting