Hey...

I've written a program that changes a single character from uppercase to lowercase and viceversa but I want to change a whole word from uppercase to lowercase and viceversa

how do I do that?

#include <cctype>
#include <iostream>

using namespace std;

int main()
{
  for (int i=1;i<=10;i++){
  
  char ch;

  cout<<"Enter a character: ";
  cin>> ch;

  if ( isalpha ( ch ) ) {
    if ( isupper ( ch ) )
      cout<<"The lower case equivalent is "<< static_cast<char> ( tolower ( ch ) ) <<endl;
    else
      cout<<"The upper case equivalent is "<< static_cast<char> ( toupper ( ch ) )<<endl;
  }
  else
    cout<<"The character is not a letter"<<endl;

 
  while ( cin.get ( ch ) && ch != '\n' )
    ;
}
 system("pause");
 return 0;
}

Go through the word character by character and change each character to upper or lower case using toupper or tolower from cctype. There is no toupper or tolower function that takes a string as a parameter and converts the whole string.

I'm sure many people have written one, but it's not a standard function.

I'm sure many people have written one

Here's mine:

#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>

struct upper {
  int operator() ( int c )  
  {
      return toupper ( static_cast<unsigned char> ( c ) );
  }
};

struct lower{
  int operator() ( int c )  
  {
      return tolower( static_cast<unsigned char> ( c ) );
  }
};

int main(){
    std::string str = "Test StrinG";
    std::transform(str.begin(),str.end(),str.begin(),upper());
    std::cout << str;
    std::transform(str.begin(),str.end(),str.begin(),lower());
    std::cout << str;
    return 0;
}

Which flavor of c++ are you using? I use VC++ for work and I use the MakeLower() function on strings.

I wasn't thinking about flavors. I was thinking about the standard libraries listed here that are available to everyone and are portable:

http://www.cplusplus.com/reference/

I know of no function in this set of functions that will do the job. But you're right. Visual C++ will have a lot of things beyond that. You can also look at the Boost functions. They could be in there too.

Edited 7 Years Ago by VernonDozier: n/a

This article has been dead for over six months. Start a new discussion instead.