Untitled

 avatar
unknown
c_cpp
2 years ago
1.1 kB
4
Indexable
#include <iostream>
#include <string>
#include <queue>

using namespace std;

int main() {
    int n;
    queue<string> words;

    // Asking for n words
    cout << "Enter the number of words: ";
    cin >> n;

    for (int i = 0; i < n; i++) {
        string word;
        cout << "Enter word " << i+1 << ": ";
        cin >> word;

        // Adding word to queue
        words.push(word);
    }

    // Inverting all the words in the queue
    int numWords = words.size();
    for (int i = 0; i < numWords; i++) {
        string word = words.front();
        words.pop();

        // Inverting the word and adding it back to the queue
        string invertedWord = "";
        for (int j = word.length()-1; j >= 0; j--) {
            invertedWord += word[j];
        }
        words.push(invertedWord);
    }

    // Displaying the content of the queue
    cout << "The inverted words are: ";
    for (int i = 0; i < numWords; i++) {
        cout << words.front() << " ";
        words.pop();
    }
    cout << endl;

    return 0;
}
Editor is loading...