Hi, I have an assignment where I need to take a string and display it in reverse order and then convert anything in upper case to lower case and vis versa. I have the first piece of the code, but I'm not sure where to begin with the second piece. I know there is a toupper and tolower and even a transform I just cant figure out how to use it...Can someone give me a clue please...I've seen some other post where toupper and tolower is used but it seems to be for just one character, so I can't make it work in my code

// ReverseString.cpp : main project file.
#include "stdafx.h"
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    const string hello("Hello, how are you?");

    string s(hello.begin(),hello.end());

    // iterate through all of the characters
    string::iterator pos;
    for (pos = s.begin(); pos != s.end(); ++pos)
	{
        cout << *pos;
    }
    cout << endl;
   // transform(s.begin(),s.end(),s.end(),tolower);
    reverse (s.begin(), s.end());
	cout << "reverse: " << s << endl;

}

I think the isalpha, islower, isupper, toupper, and tolower functions from the cctype library will come in handy here:

http://www.cplusplus.com/query/search.cgi?q=cctype

Isolate each character, then test that character with the isalpha function to see if it is a letter. If it IS NOT a letter, just display the character. IF it IS a letter, use isupper or islower to check whether it is upper- or lower-case. Depending on which it is, use the tolower or toupper function to convert it to upper or lower case, whichever is appropriate, then display that letter.

You already have each character isolated with *pos, so go from there:

char a = *pos;
if (isalpha (a))
{
     // code to alter case of a
}
cout << a;

Thank you.
okay I tried, this is not to say I did it right of course. But I got a string iterator can not be dereferenced failure when debugging...So here is what I have now:

// ReverseString.cpp : main project file.
#include "stdafx.h"
#include <string>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <ctype.h>

using namespace std;

int main()
{
    const string hello("Hello, how are you?");

    string s(hello.begin(),hello.end());

    // iterate through all of the characters
    string::iterator pos;
    for (pos = s.begin(); pos != s.end(); ++pos)
	{
        cout << *pos;
    }
		char a = *pos;
		if (isalpha (a))
		{
		if ( isupper ( a ) ) 
		{
		a = tolower ( a );

		 }
		cout << a;
		
		cout << endl;
	   // transform(s.begin(),s.end(),s.end(),tolower);
		reverse (s.begin(), s.end());
		cout << "reverse: " << s << endl;

}
	cout << endl;
   // transform(s.begin(),s.end(),s.end(),tolower);
    reverse (s.begin(), s.end());
	cout << "reverse: " << s << endl;

}

:) Got it !! Thank you again VernonDozier

#include "stdafx.h"
#include <string>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <ctype.h>

using namespace std;

int main()
{
    const string hello("Hello, how are you?");
	cout << hello<<endl;
    string s(hello.begin(),hello.end());
	reverse (s.begin(), s.end());
	cout << "string in reverseorder: " << s << endl;

    // iterate through all of the characters
    string::iterator pos;
    for (pos = s.begin(); pos != s.end(); ++pos)
		
	{
        int i = 0;
		char a = *pos;
		if (isalpha (a))
		//{
		//while ( isalpha( a )  )
			{
				if (isupper(a)) 
				{
				*pos = tolower (a);
				//putchar (a);
				i++;
				}
				
				if (islower(a)) 
				{
				*pos = toupper (a);
				//putchar (a);
				i++;
				}
				}
		}
		cout << "string in reverseorderChangeCase: " << s << endl;
		reverse (s.begin(), s.end());
		cout << s << endl;
		
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.