I have this assignment:
Write a program with three functions: upper, lower, and reverse.
The upper function will accept a pointer to a string or C-string as an argument. It will step through each character in the string converting it (where necessary) to uppercase.
The lower function will accept a pointer to a string or C-string as an argument. It will step through each character in the string converting it (where necessary) to lowercase.
The reverse function will accept a pointer to a string or C-string. It will step through each character in the string converting uppercase characters to lowercase and lowercase characters to uppercase; numbers and special characters will be unaffected.
The program will execute the functions in the following order: upper, lower, and reverse.
When entering "AbCd", the upper function will return "ABCD"; the lower function will return "abcd"; and the reverse function will return "aBcD"
I beleive what is happening is that when function lower or reverse runs, it's not taking the orginal imput and changing it, but taking the imput from the last function. How do I prevent this?
#include <iostream>
#include <cctype>
using namespace std;
//function prototype
void upper(char *, const int);
void lower(char *, const int);
void reverse(char *, const int);
int main()
{
// Constant for the size of a line of input
const int SIZE = 26;
// Array to hold a line of input
char letters1[SIZE];
// Prompt the user to enter a string.
cout << "Please enter some upper and/or lower case letters (25 Characters Max): \n";
cin.getline(letters1, SIZE);
cout << "\n";
upper(letters1, SIZE);
lower(letters1, SIZE);
reverse(letters1, SIZE);
system("pause");
return 0;
}
//Uppper function
void upper(char *letters2, int count)
{
// Convert all lowercase characters to uppercase
for (int count = 0; count < strlen(letters2); count++)
{
// convert it to an uppercase character.
if (islower(letters2[count]))
{
letters2[count] = toupper(letters2[count]);
}
}
// Display the converted line of input.
cout << letters2 << endl;
return;
}
//lower function
void lower(char *letters3, const int size)
{
// Convert all lowercase characters to uppercase
for (int count = 0; count < strlen(letters3); count++)
{
// Convert it to an lowercase character.
if (isupper(letters3[count]))
{
letters3[count] = tolower(letters3[count]);
}
}
// Display the converted line of input.
cout << letters3 << endl;
return;
}
// reverse function
void reverse(char *letters4, const int size)
{
// Convert all lowercase characters to uppercase
for (int count = 0; count < strlen(letters4); count++)
{
if (isupper(letters4[count]))
letters4[count] = tolower(letters4[count]);
else
if(islower(letters4[count]))
letters4[count] = toupper(letters4[count]);
}
cout << letters4 << endl;
}