Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8239723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:14:33+00:00 2026-06-07T20:14:33+00:00

I am practising Koeing accelerated C++ , and want to verify my answer. Since

  • 0

I am practising Koeing accelerated C++ , and want to verify my answer. Since there are no solutions available on net , I thought to post it here and ask experts view about my solution. I am not sure if people will like me to post it here. if not , please let me know and i will not do it in future. Also to mention, its not homework , its purely my desire to take my C++ skills to next level.

Question: Write a program to find all the palindromes in a dictionary. Next, find the longest palindrome.

What I have done so far -> I have defined function to test palindrome , also stored all words in a list. I have posted my code below.

Where I am stuck: I need advice , whether my choice of using list data structure over vector is good or not? Secondly, I am stuck how to display longest word. I can display longest length but not longest word.

my attempt below

    bool palindromeTest( const std::string& input )
{
   typedef std::string::size_type strSize;
    strSize i = 0;
    strSize j = input.size() - 1  ;
    while( i < input.size() )
    {
        if ( input[i] != input[j])
        {
            return false;
        }
        i++;
        j--;
    }

    return true;
}



int main()
{

    // stores all words in a list or vector
    std::list< string> listDict;
    std::string readWord;
    std::ifstream readFile( "/Users/apple/palidndrome-ch5-10/dict.txt" );
    if( ! readFile )
    {
        std::cout <<" failed to open file" << std::endl;
        return 0;
    }
    while( readFile >> readWord )
    {
        listDict.push_back( readWord );
    }

     std::string::size_type maxLen = 0 ;
     std::string longestWord = " "; // to store longest palindrome


     // print all the palindrome words and also which is longest palindrome.

    for( std::list<std::string>::const_iterator it = listDict.begin(); it != listDict.end(); ++it )
    {

        if( palindromeTest( *it )  )
        {
            std::cout <<" the word -> " << *it << " is palindrome" << std::endl;
            // find max len of palindrome;
            maxLen = max( maxLen, it->size() );
            longestWord = *it ;// need to change code here ?? no idea how

        }

    }

    std::cout <<" the maximum len is = " << maxLen << std::endl;
    std::cout << " the word with maximum length is  " << longestWord ; // something is wrong here


    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:14:35+00:00Added an answer on June 7, 2026 at 8:14 pm

    Vectors and lists both work equally well here, though a vector is more efficient.

    You can find the longest word by changing

    maxLen = max( maxLen, it->size() );
                longestWord = *it ;// need to change code here ?? no idea how
    

    to

    if (it->size() >= longestWord.size()) {longestWord = *it;}
    

    (You don’t actually need to keep track of maxlen since you can just call longestWord.size().)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am once again a bit stuck in my practising. I want an MP3
hey there, just practising and I had a question. I have a program (source
I know there are duplicates of this question, and the answer was to refactor
I'm practising a bit on JSP and want to create a simple formular field.
I was practising statements and ran into a problem. I'm trying to make it
I've recently started practising TDD and unit testing, with my main primers being the
I was practising the algorithm based programming problem.I am having difficulty,in solving this problem.I
Started practicing with XML and C# and I have an error message of There
I am a newbie to WPF. I am practising the sample code in WPF
I've been playing around with Haskell a fair bit, including practising writing functions in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.