How do you change the appearance of the output?

Hi,

I'm trying to figure out how to display input backwards, uppercase, lowercase, and display the middle letter of the input without using the tolower() method, toupper() method, vectors, or any other method that reverses the sentence automatically. I can't find too much help whenever I search because everyone wants to just use the easier options I'm not allowed to use.

I have a program, but since I can't figure out the functions needed, some places are blank.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

#include "pch.h"
#include "string"
#include "iostream"
#include "iomanip" //used for setw
using namespace std;

int main()
{
	string sentence;
	string middle;
	int numChoice;
	bool validInput = false;

	cout << "Welcome to my program. Enter a sentence and select one of the options below.";
	cout << "\nEnter -999 to exit the program.";
	cout << "\n============================================================================\n";
	cout << "1. Display the middle character, if there is one.\n";
	cout << "2. Convert to uppercase.\n";
	cout << "3. Convert to lowercase.\n";
	cout << "4. Display backwards.\n";

	cout << "\nEnter a sentence: ";
	cin.ignore();
	getline(cin, sentence);

	cout << "Selection: ";
	cin >> numChoice;

	{
		if (numChoice >= 1 && numChoice <= 4)
		{
			validInput = true;
		}
		else
		{
			validInput = false;
		}
		while (!validInput)
		{
			cout << "Invalid input. Try again. Selection: ";
			cin >> numChoice;

			if (numChoice >= 1 && numChoice <= 4)
			{
				validInput = true;
			}
			else
			{
				validInput = false;
			}
		}
	}

	if (numChoice == 1)
	{
		cout << "     MIDDLE     \n";
		cout << "================\n";
	}
	else if (numChoice == 2)
	{
		cout << "    UPPERCASE   \n";
		cout << "================\n";
	}
	else if (numChoice == 3)
	{
		cout << "    LOWERCASE   \n";
		cout << "================\n";
	}
	else if (numChoice == 4)
	{
		cout << "    BACKWARDS   \n";
		cout << "================\n";
		reverse(sentence.begin(), sentence.end());
	}

	system("pause");
	return(0);
}
Last edited on
To output a string backwards you can just use a for loop starting at the end and going to the beginning.
For uppercase and lowercase you need to write your own function if you are not allowed to use toupper and tolower. For the middle letter you just divide the length of the string by 2 and display that character.
Here are some tips.

Like Thomas said, you just have to use a for-loop for displaying the string backwards. The initial value must be the size of the string minus one (str.length()-1), print while the iterating variable isn't less than 0 and update the iterating variable by decrementing it by 1. Simple init?

For tolower() and toupper(), let's make an observation. Capital case 'A' has the ASCII value 65. And lower case 'a' has the ASCII value of 97. Now remember that these are integer values so you can perform mathematical operations on them. The difference between 'A' and 'a' is 32. So if a character is lower case (if it's within the range of 65 and 90, 90 is for 'z') and you need to convert it to upper case, add 32. If it's within the range 97 to 122 then don't do anything. If it's neither then it's not an alphabet so don't do anything. Likewise for converting to lower case but with subtraction instead of addition. So do this all in a for-loop, again.

For finding the middle letter, use str.length, again, to find the length of the string. If it's an even number then there is no single 'middle letter' but instead 2. So if it's even you find ((str.length/2)-1) and ((str.length/2)), these both indexes are your middle letters so print them. If it's odd then just find ((str.length+1/2)-1) and that is your middle index for a string with odd number of characters. The -1 is because arrays start with the index 0.


Last edited on
there are a bunch of ways to do these things.
lookup tables, for example...
unsigned char upr[256];
for(x = 0; x < 256; x++)
upr[x] = x;
for(c = 'A', x = 'a'; x <= 'z'; x++)
upr[x] = c++;

and then you can toupper dirrectly:
stringy[index] = upr[stringy[index]]; //all a-z become A-Z.

you can also do it with math, as the offset between A and a is a constant and a-z and A-Z are in english sequential order in the ascii table. lower can be done the same way; any one to one substitution can be done this way -- we use it at work to cull unprintable chars from damaged data.

middle letter is subjective, for abcd what is the middle? regardless its stringy[length/2 +offset] where offset is 0, 1, or -1 depending on how you defined middle, and whether the length is even or odd possibly coupled with your definition.

reverse is just iteration... go from stringy[length-1] to zero in a loop and do what you want with the data.

lookup tables fall flat for unicode though. dealing with that poorly thought out mess takes a great deal of patience and extra logic. Hopefully you won't ever try to DIY unicode...

"" for your headers instead of <> is disturbing. People follow different conventions there, but most use <> for the build in ones.
Last edited on
Topic archived. No new replies allowed.