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.
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.
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