i'm trying to enter a string phrase and store its last word in an Array,
if the word is palindrom then a message will showit

Dani AI

Generated

Short practical plan (what wanted): read the whole phrase, extract the last token (not just the first word), normalize it (lowercase and remove non-alphanumeric characters), then compare it to its reverse to decide palindrome. asked for runnable examples; was right that splitting is a common approach, but a right-split or "find last word" method is safer than using operator>> — as 's toy example shows, cin >> only reads the first token.

Python (concise, handles trailing spaces and punctuation):

# Python 3
phrase = input().strip()
last = phrase.rsplit(None, 1)[-1] if phrase else ''
clean = ''.join(ch.lower() for ch in last if ch.isalnum())
print("palindrome" if clean and clean == clean[::-1] else "not palindrome")

C++ (reads whole line, extracts last token robustly, strips non-alnum and checks case-insensitively):

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
    std::string line;
    std::getline(std::cin, line);
    auto end = line.find_last_not_of(" \t");
    if (end == std::string::npos) return 0;
    auto pos = line.find_last_of(" \t", end);
    std::string last = (pos == std::string::npos) ? line.substr(0, end + 1)
                                                 : line.substr(pos + 1, end - pos);
    std::string clean;
    for (unsigned char ch : last) if (std::isalnum(ch)) clean.push_back(std::tolower(ch));
    bool is_pal = !clean.empty() && std::equal(clean.begin(), clean.end(), clean.rbegin());
    std::cout << (is_pal ? "palindrome\n" : "not palindrome\n");
}

Notes and quick troubleshooting: reading the full line is important (use getline / input()), otherwise the phrase is cut at the first space. Decide whether digits count and whether punctuation should be ignored (examples above ignore punctuation and use case-insensitive checks). For international/Unicode text, the simple isalnum/tolower approach works only for ASCII — proper Unicode normalization requires a dedicated library. If the last word must be stored, push it into a container (Python list or C++ vector) after extraction.

Recommended Answers

All 3 Replies

Please post some code and ask a more specific question with a problem you are having with YOUR code.

Member Avatar for Member #1106139

mahabelg you can do following but do give a try first:

try to split and store the read input into an array or list, i believe you want to get that via console to give a hint here is some example:

var input = Console.ReadLine();
string [] arr = new string[] { };
if(input.Length > 1) {
    for(int ind=0; ind < input.Length-1; ind++) {
        arr[ind] = input.split(' ')[ind];
     }
}

next when you do have the inputs in your array meaning at this point you could have something like:

arr[0] contains "inputstring1"
arr[1] contains "inputstring2"

you can start to check for palindromes write a separate method why won't you!! check after reversing the individual input strings and if their reversed version is equal to the original version the one which you extracted in the array as described just above you can confirm whether read string was a palindrome or otherwise!!

Happy coding !! :)

Member Avatar for Member #46692
#include <iostream>
#include <string>

using namespace std;

int main()
{

    string phrase;
    cin>>phrase;

    if (phrase == "palindrome")
    {
        cout << "showit";

    }
    else
    {
        cout <<"nope";
    }

    return 0;

}

Couldn't resist.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.