Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,978 questions

51,922 answers

573 users

How to print the longest palindrome words in a string with C++

2 Answers

0 votes
#include <iostream>
#include <sstream>
  
void PrintLongestPalindromeWord(std::string str) {
    std::istringstream ss(str);
    std::string word; 
    size_t max = 0;
    std::string maxword;
    
    while (ss >> word) {
        std::string rev_word(word.rbegin(), word.rend());
        int wordlen = word.size();
        if (word == rev_word && wordlen >= 3) {
            if (wordlen > max) {
                max = wordlen;
                maxword = word;
            }
        }
    }
    
    std::cout << maxword << "\n";
}
    
  
int main()
{
    std::string str = "c madam c++ civic java rotator pytyp dart php";
      
    PrintLongestPalindromeWord(str);
}
 
  
  
  
/*
run:
          
rotator
     
*/

 



answered Dec 29, 2023 by avibootz
0 votes
#include <iostream>
#include <sstream>
   
std::string  GetTheLongestPalindromeWord(std::string str) {
    std::istringstream ss(str);
    std::string word; 
    size_t max = 0;
    std::string maxword;
     
    while (ss >> word) {
        std::string rev_word(word.rbegin(), word.rend());
        int wordlen = word.size();
        if (word == rev_word && wordlen >= 3) {
            if (wordlen > max) {
                max = wordlen;
                maxword = word;
            }
        }
    }
     
    return maxword;
}
     
   
int main()
{
    std::string str = "c madam c++ civic java rotator pytyp dart php";
       
    std::string maxword = GetTheLongestPalindromeWord(str);
    
    std::cout << maxword << "\n";
}
  
   
   
   
/*
run:
           
rotator
      
*/

 



answered Dec 29, 2023 by avibootz

Related questions

1 answer 124 views
2 answers 120 views
1 answer 85 views
1 answer 83 views
1 answer 126 views
3 answers 149 views
2 answers 133 views
...