I am struggling with this assignment to long i need someone to tell me what am i doing wrong

Write a program with three functions: upper, lower, and reverse.

The upper function should accept a pointer to a c-string as an argument. It should step through each character in the string, converting it to uppercase.

The lower function, to should accept a pointer to a c-string as an argument. It should step through each character in the string converting it to lower case.

Like upper and lower, reverse should also accept a pointer to a c-string. As it steps through the string, it should test each character to determine whether it is upper or lowercase. If a character is uppercase, it should be converted to lowercase. Likewise, if a character is upper case it should be converted to lowercase. Likewise, if a character is lowercase is should be converted to uppercase.

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

char* reverse (char*, int);
char* lower (char*, int);
char* upper (char*, int);

int main ()
{
const int SIZE = 80;
char myString [SIZE];
int strLength;

//Prompt user for string
cout << "Please enter a string.\n";

//read in string
cin.getline(myString, SIZE);

//find and display length
strLength = strlen(myString);
cout << "Your string is " << strLength << " characters long\n";


//call reverse function and output results in main to screen
	reverse(myString, strLength);

	cout << "Your string reversed: ";
	for (int count = 0; count < strLength; count++)
	cout << reverse(myString, strLength);
	cout << myString[count] = tolower;
	cout << endl;

	//call lower function and output results in main to screen
	lower(myString, strLength);

	cout << "Your string in lowercase: ";
	for (int count = 0; count < strLength; count++);
	cout << myString[count] ;
	cout << endl;

	//call upper function and output results in main to screen
	upper(myString, strLength);

	cout << "Your string in uppercase: ";
	for (int count = 0; count < strLength; count++)
	cout << myString[count];
	cout << endl;

	return 0;


	// function that reverses the case of string
	char* reverse (char * string, int SIZE)
	
		for(int count = 0; count < SIZE; count++)
		{
			if (isupper(string[count]))
			tolower(string[count]);
			else if (islower(string[count]))
			toupper(string[count]);
		}
		
		return string;



		// function that puts entire string in lower case
		char* lower (char * string, int SIZE)
		{
		for(int count = 0; count < SIZE; count++)
		tolower(string[count]);
		return string;
		}

		// function that puts entire string in upper case
			char* upper (char * string, int SIZE)
			{
			for(int count = 0; count < SIZE; count++)
				toupper(string[count]);
			return string;
			}
}

Recommended Answers

All 14 Replies

You are trying to define your functions in a function. Check your braces on each one.

Hi Chilton
can you tell me what lines i getting some errors about the [count]

You'll have to look through. Start from main. Make sure you close it properly, then continue until each function is separate.

Change case to upper: toupper(int c) - loop through the string and apply this to each character.

Change case to lower: tolower(int c) - ditto

Reverse case: if isupper(int c) tolower(c), if islower(c) toupper(c)

You figure out the details.

i tried that still cannot compile any more clues ??

Post your compiler error messages here, along with your current code.

Hello shyla,
You didn't enclose the for loop with braces on line 30.

cout << "Your string reversed: ";
	for (int count = 0; count < strLength; count++)
	cout << reverse(myString, strLength);
	cout << myString[count] = tolower;

Since you didn't do that, it will print the string sent by reverse() several times.

I tried to arrange it as much as i can, it compile but everything is overlap
see the code below

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

char* reverse (char*, int);
char* lower (char*, int);
char* upper (char*, int);

int main ()
{
	const int SIZE = 80;
	char myString [SIZE];
	int strLength;

	//Prompt user for string
	cout << "Please enter a string.\n";
	//read in string
	cin.getline(myString, SIZE);
	//find and display length
	strLength = strlen(myString);
	cout << "Your string is " << strLength << " characters long\n";


	//call reverse function and output results in main to screen
	reverse(myString, strLength);
	{
		cout << "Your string reversed: ";
		for (int count = 0; count < strLength; count++)
		{
		cout << reverse(myString, strLength);
		cout << myString[count];
		cout << endl;
		}
	}

	//call lower function and output results in main to screen
	lower(myString, strLength);
	{
		cout << "Your string in lowercase: ";
		for (int count = 0; count < strLength; count++)
		{
	
			cout << lower(myString, strLength);
			cout << myString[count] ;
			cout << endl;
		}
	}
	
	//call upper function and output results in main to screen
	upper(myString, strLength);
	{
	cout << "Your string in uppercase: ";
		for (int count = 0; count < strLength; count++)
		{
			cout << myString[count];
			cout << endl;
		}
	}
}
// function that reverses the case of string
	char* reverse (char * string, int SIZE)
	{
		for(int count = 0; count < SIZE; count++)
		{
			if (isupper(string[count]))
			tolower(string[count]);
			else if (islower(string[count]))
			toupper(string[count]);
			return string;
		}
	}

	// function that puts entire string in lower case
	char* lower (char * string, int SIZE)
	{
		for(int count = 0; count < SIZE; count++)
		{
			tolower(string[count]);
			return string;
		}
	}



	// function that puts entire string in upper case
	char* upper (char * string, int SIZE)
	{
		for(int count = 0; count < SIZE; count++)	
		{
			toupper(string[count]);
			return string;
		}	
	}

You have given all the lines in the for loop. I meant only the lines you want.

char* reverse (char * string, int SIZE)
	{
		for(int count = 0; count < SIZE; count++)
		{
			if (isupper(string[count]))
			tolower(string[count]);
			else if (islower(string[count]))
			toupper(string[count]);
			return string;
		}
	}

A function can return only one value. You should not give return inside the for loop.

char* reverse (char * string, int SIZE)
	{
		for(int count = 0; count < SIZE; count++)
		{
			if (isupper(string[count]))
			tolower(string[count]);
			else if (islower(string[count]))
			toupper(string[count]);
		
		}
    return(string);
	}

In the code below you have given the string mycount inside the for loop which does not get changed at all in the upper function.

upper(myString, strLength);
	{
	cout << "Your string in uppercase: ";
		for (int count = 0; count < strLength; count++)
		{
			cout << myString[count];
			cout << endl;
		}
	}

Instead of the above code above give

cout<<upper(mystring, strlength);

And also in the function upper and lower when you are using tolower and toupper functions you should give

mystring[count]=touppper(mystrting[count]);

Then only mycount will have the character changed to uppercase or lowercase.

i am trying to fix it with your suggestions but i still haven't got the righe solution

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

char* reverse (char*, int);
char* lower (char*, int);
char* upper (char*, int);

int main ()
{
	const int SIZE = 80;
	char myString [SIZE];
	int strLength;

	//Prompt user for string
	cout << "Please enter a string.\n";
	//read in string
	cin.getline(myString, SIZE);
	//find and display length
	strLength = strlen(myString);
	cout << "Your string is " << strLength << " characters long\n";

	//call reverse function and output results in main to screen
	reverse(myString, strLength);
	{
		cout << "Your string reversed: ";
		for (int count = 0; count < strLength; count++)
		{
		cout << reverse(myString, strLength);
		cout << myString[count];
		cout << endl;
		}
	}

	//call lower function and output results in main to screen
	lower(myString, strLength);
	{
			cout << "Your string in lowercase: ";
		for (int count = 0; count < strLength; count++)
		{
		
			 //cout << lower(myString, strLength);
			 myString[count] =  myString[count] = tolower(myString[count]);
			 cout << endl;
		}
	}
	
	//call upper function and output results in main to screen
	upper(myString, strLength);
	{
		//cout << "Your string in uppercase: ";
		for (int count = 0; count < strLength; count++)
		{
			 myString[count] = toupper(myString[count]);
			cout << endl;
		}
	}
}
// function that reverses the case of string
	char* reverse (char * string, int SIZE)
	{
		for(int count = 0; count < SIZE; count++)
		{
			if (isupper(string[count]))
			tolower(string[count]);
			else if (islower(string[count]))
			toupper(string[count]);
		}
		return string;
	}

	// function that puts entire string in lower case
	char* lower (char * string, int SIZE)
	{
		for(int count = 0; count < SIZE; count++)
		{
			tolower(string[count]);
			
		}
		return string;
	}




	// function that puts entire string in upper case
	char* upper (char * string, int SIZE)
	{
		for(int count = 0; count < SIZE; count++)	
		{
			toupper(string[count]);
			
		}	
		return string;
	
	}

I hope you are printing the reversed string in the block(line 25-30).
You have given cout<<reverse(mysring, strlength) inside the loop and that's why you get the string printed multiple times. Give this instead of lines 25-34.

cout << "Your string reversed: ";
cout<<reverse(mystring, strlength);
for (int count = 0; count < strLength; count++)
{ cout << myString[count];
}
 cout<<endl;

Try changing line 37-47 with this...
cout << "Your string in lowercase: ";

cout<<lower(mystring, strlength);
for (int count = 0; count < strLength; count++)
{
 cout<<mystring[count];
}
cout<<endl;

similarly try doing this to line 50-58.
In my previous post i gave you an example of how to use tolower and toupper functions. Try changing it in the lower, upper and reverse functions.
An example...

char* lower (char * string, int SIZE)
{
 for(int count = 0; count < SIZE; count++)
 {
  string[count]= tolower(string[count]);
 }
 return string;
}

Hope this helps.

Arbus you are an excellent programmer very professional thank you very much,
what is the purpose of the reverse? to switch from lower to upper and vice versa ?

Arbus you are an excellent programmer very professional

Thank you.

what is the purpose of the reverse? to switch from lower to upper and vice versa ?

Yes, if the character is in lower case, then it changes it to upper case and vice versa. The reverse function changes the case of the character. So the string will have alternate upper and lower case characters.

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.