[nfbcs] C++ question

jim Barbour jbar at barcore.com
Mon Nov 12 04:11:13 UTC 2018


Here's some example code...

#include <string>
 
using namespace std;
 
string strtolower(string str);
string strtoupper(string str);
 
int main(int argc, char *argv[])
{
    cout << strtoupper("loWer") << endl;//LOWER
    cout << strtolower("loWer") << endl;//lower
    cout << strtoupper("UPPER") << endl;//UPPER
    cout << strtolower("UPPER") << endl;//upper
    cout << strtoupper("`az{") << endl; //`AZ{
    cout << strtolower("@AZ[") << endl; //@az[
}
 
string strtoupper(string str)
{
    int leng=str.length();
    for(int i=0; i<leng; i++)
        if (97<=str[i]&&str[i]<=122)//a-z
            str[i]-=32;
    return str;
}
string strtolower(string str)
{
    int leng=str.length();
    for(int i=0; i<leng; i++)
        if (65<=str[i]&&str[i]<=90)//A-Z
            str[i]+=32;
    return str;
}



On Sun, Nov 11, 2018 at 11:03:54PM -0500, Joseph C. Lininger via nfbcs wrote:
> I originally didn't copy the list, but I decided this might be useful for
> everyone. So, here's my response to this message.
> 
> 
> What you'll want depends on whether or not you're using the older style C
> strings or the newer C++ style string objects. I'm assuming, because you
> said C++, that it's string objects you're using. In that case, first make
> sure you have #included the string header, like this:
> 
> #include <string>
> 
> You would have had to do that anyway to make the string object available,
> but figured I'd mention it anyway. While you're at the top of your file,
> also make sure you have this line. You'll need it for the transformation I'm
> going to show you.
> 
> #include <algorithm>
> 
> Finally, make sure you have:
> 
> using namespace std;
> 
> Now, for the case conversion. This problem is, unfortunately, harder than it
> should be. You have two choices.
> 
> 1. Apply a transformation to the String, whereby you use the transform()
> function in conjunction with the old C toupper() function to convert each
> character to uppercase. (You have to use the old C toupper, not the one from
> C++, which I'll show you how to do.)
> 
> 2. Convert the C++ string to a traditional C null-terminated string (char *
> if you're a C guy), then use strupper to convert that to upper case. Be
> careful with memory locations and such if you elect to do this.
> 
> I'll show how to do it using option 1 since that doesn't require you ever
> convert the String object to something else. Assuming you have a string
> called theString, you would write a line like this to convert it to
> uppercase using transform().
> 
> transform(theString.begin(), theString.end(), theString.begin(), ::toupper);
> 
> Here's a sample program that reads a string, then converts to uppercase and
> displays it. I actually compiled and ran this, so I know it works at least
> on my setup.
> 
> #include <iostream>
> #include <string>
> #include <algorithm>
> 
> using namespace std;
> 
> int main()
> {
>     string theString;
>     cout << "Enter a string: ";
>     cin >> theString;
>     // The magic is the next line
>    transform(theString.begin(), theString.end(), theString.begin(),
> ::toupper);
>     cout << "The string is: " << theString << "\n";
> }
> 
> Hope this helps.
> Joe
> 
> On 11/11/2018 20:19, gwunder--- via nfbcs wrote:
> > My grandson has asked for his grandfather's help, and although I have not
> > coded in C++, so far I've been able to help. We are stumped by the need to
> > convert a string to one case. It matters not at all to us if that is upper
> > or lower case. We need to see if a string read forward or backward is the
> > same-a palindrome. Though we find a function called toupper, we do not find
> > the library in which it resides. It appears not to work on more than one
> > character at a time.
> > 
> > 
> > So if any of you can write me off list, how do we convert a string to one
> > case?
> > 
> > 
> > Thank you.
> > 
> > 
> > 
> > _______________________________________________
> > nfbcs mailing list
> > nfbcs at nfbnet.org
> > http://nfbnet.org/mailman/listinfo/nfbcs_nfbnet.org
> > To unsubscribe, change your list options or get your account info for nfbcs:
> > http://nfbnet.org/mailman/options/nfbcs_nfbnet.org/devnull-nfbcs%40pcdesk.net
> 
> _______________________________________________
> nfbcs mailing list
> nfbcs at nfbnet.org
> http://nfbnet.org/mailman/listinfo/nfbcs_nfbnet.org
> To unsubscribe, change your list options or get your account info for nfbcs:
> http://nfbnet.org/mailman/options/nfbcs_nfbnet.org/jbar%40barcore.com
> 




More information about the NFBCS mailing list