changeing each lowercase letter to uppercase

I have to write a program that reads strings of text from a datafile called “data.txt”, changes each lowercase letter to uppercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome. I'm currently not understanding how I could write a function that can change each lowercase letter to uppercase, nor how I would take each letter into a queue and into a stack.
What have you tried so far?

Note, the tolower and toupper functions operate on single characters, and are available within the <cctype> header.
https://www.cplusplus.com/reference/cctype/tolower/
https://www.cplusplus.com/reference/cctype/toupper/

Stacks and queues are both part of the standard library:
https://www.cplusplus.com/reference/queue/queue/
https://www.cplusplus.com/reference/stack/stack/

A queue is "FIFO" (First in, First Out), while a stack is "LIFO" (Last in, First out).

As you push to both the queue and the stack, they will increase in size. Popping from a queue maintains the order of the characters, while popping from a stack effectively reverses the order of the characters. So if you pop from the queue each time you pop from the stack, and compare the letters for equality, that is a possible method for checking if the text is a palindrome.
Last edited on
I haven't done queues and stacks in a long time, so can't help you there, sorry :(

But I would read the cplusplus.com references on them like @Ganado suggested. And maybe find some other stuff too.
https://en.cppreference.com/w/cpp/container/queue

To convert to uppercase:
Like @Ganado said, use toupper() and tolower() to convert. However, they only convert a single char at a time!

So what you want to do is use a for loop to go through the stack or queue and convert each letter to uppercase. I'm not sure if there's another way to do it; this is the way you would do it for an array or a vector or a list.

e.g. (This is for a C-string, NOT a queue or stack)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <ctype.h>
#include <string.h>

int main ()
{
     char cstring[] = {'a', 'b', 'c', 'd', 'e', 'f'};
     
     puts (cstring); // test c-string
     
     for (size_t i = 0; i <= sizeof (cstring); ++i)
     {
          cstring[i] = std::toupper (cstring[i]);
     }
     
     puts (cstring); // output uppercase c-string
     
     return 0;
}


Note: This actually modifies the C-string. If you don't want to change the C-string, but you want to display it as uppercase, replace line 12 with
 
putchar (std::toupper (cstring[i]));

and that will simply display it as all uppercase.

Good luck!
max
Using a queue and a stack to test that a word is a palindrome is far more complicated than needed. However, consider:

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
#include <queue>
#include <stack>
#include <string>
#include <iostream>
#include <cctype>

bool ispalin(const std::string& word)
{
	std::queue<char> qc;
	std::stack<char > sc;

	for (const auto& c : word) {
		const auto uc {static_cast<char> (std::toupper(static_cast<unsigned char>(c)))};

		qc.push(uc);
		sc.push(uc);
	}

	bool palin {true};

	for (; palin && !qc.empty() && !sc.empty(); qc.pop(), sc.pop())
		palin = qc.front() == sc.top();

	return palin && qc.empty() && sc.empty();
}

int main()
{
	if (ispalin("oxo"))
		std::cout << "oxo is a palindrome\n";
	else
		std::cout << "oxo is not a palindrome\n";

	if (ispalin("palin"))
		std::cout << "palin is a palindrome\n";
	else
		std::cout << "palin is not a palindrome\n";
}



oxo is a palindrome
palin is not a palindrome

Last edited on
Topic archived. No new replies allowed.