View Single Post
Old 10-14-2004, 06: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 01:53 PM.
Valmont is offline   Reply With Quote