原文:

给定一个字符串 str ,任务是打印字符串 str 中存在的最长回文单词。 例:

输入:阿罗拉夫人教马拉雅拉姆语 输出:马拉雅拉姆语 解释:该字符串包含三个回文单词(即夫人、阿罗拉、马拉雅拉姆语),但马拉雅拉姆语的长度大于其他两个。 输入:欢迎使用 geeksforgeeks 输出:无回文词 解释:字符串不包含任何回文词,所以输出为无回文词。

进场:

  • longestpalin() 函数通过提取字符串的每个单词并将其传递给 checkpalin()函数来查找最长的回文单词。在原始字符串中添加额外的空格来提取最后一个单词。
  • checkpalin() 函数检查单词是否是回文。如果单词是回文,则返回 true,否则返回 false。它确保空字符串不会被视为回文,因为用户可能会在字符串之间或开头输入多个空格。

c

/* c   program to print longest palindrome
word in a sentence and its length*/
#include 
#include 
#include 
using namespace std;
// function to check if a
// word is palindrome
bool checkpalin(string word)
{
    int n = word.length();
    // making the check case
    // case insensitive
    // word = word.tolowercase();
    transform(word.begin(), word.end(),
              word.begin(), ::tolower);
    // loop to check palindrome
    for (int i = 0; i < n; i  , n--)
        if (word[i] != word[n - 1])
            return false;
    return true;
}
// function to find longest
// palindrome word
string longestpalin(string str)
{
    // to check last word for palindrome
    str = str   " ";
    // to store each word
    string longestword = "", word = "";
    int length, length1 = 0;
    for (int i = 0; i < str.length(); i  )
    {
        char ch = str[i];
        // extracting each word
        if (ch != ' ')
            word = word   ch;
        else {
            length = word.length();
            if (checkpalin(word) &&
                       length > length1)
            {
                length1 = length;
                longestword = word;
            }
            word = "";
        }
    }
    return longestword;
}
// driver code
int main()
{
    string s = "my name is ava and i love"
                         " geeksforgeeks";
    if (longestpalin(s) == "")
        cout<<"no palindrome"<<" word";
    else
        cout<

java 语言(一种计算机语言,尤用于创建网站)

/*java program to print longest palindrome
word in a sentence and its length*/
public class gfg {
    // function to check if a
    // word is palindrome
    static boolean checkpalin(string word)
    {
        int n = word.length();
        // making the check case
        // case insensitive
        word = word.tolowercase();
        // loop to check palindrome
        for (int i = 0; i < n; i  , n--)
            if (word.charat(i) !=
                       word.charat(n - 1))
                return false;
        return true;
    }
    // function to find longest
    // palindrome word
    static string longestpalin(string str)
    {
        // to check last word for palindrome
        str = str   " ";
        // to store each word
        string longestword = "", word = "";
        int length, length1 = 0;
        for (int i = 0; i < str.length(); i  )
        {
            char ch = str.charat(i);
            // extracting each word
            if (ch != ' ')
                word = word   ch;
            else {
                length = word.length();
                if (checkpalin(word) &&
                             length > length1)
                {
                    length1 = length;
                    longestword = word;
                }
                word = "";
            }
        }
        return longestword;
    }
    // driver code
    public static void main(string args[])
    {
        string s = new string("my name is ava "
                  "and i love geeksforgeeks");
        if (longestpalin(s) == "")
            system.out.println("no palindrome"
                              " word");
        else
            system.out.println(longestpalin(s));
    }
}

python 3

# python 3 program to print longest palindrome
# word in a sentence and its length
# function to check if a word is palindrome
def checkpalin(word):
    n = len(word)
    # making the check case
    # case insensitive
    word = word.lower()
    # loop to check palindrome
    for i in range( n):
        if (word[i] != word[n - 1]):
            return false
        n -= 1
    return true
# function to find longest
# palindrome word
def longestpalin(str):
    # to check last word for palindrome
    str = str   " "
    # to store each word
    longestword = ""
    word = ""
    length1 = 0
    for i in range(len(str)):
        ch = str[i]
        # extracting each word
        if (ch != ' '):
            word = word   ch
        else :
            length = len(word)
            if (checkpalin(word) and
                length > length1):
                length1 = length
                longestword = word
            word = ""
    return longestword
# driver code
if __name__ == "__main__":
    s = "my name is ava and i love geeksforgeeks"
    if (longestpalin(s) == ""):
        print("no palindrome word")
    else:
        print(longestpalin(s))
# this code is contributed by ita_c

c

/* c# program to print longest palindrome
word in a sentence and its length*/
using system;
class gfg
{
    // function to check if a
    // word is palindrome
    static bool checkpalin(string word)
    {
        int n = word.length;
        // making the check case
        // case insensitive
        word = word.tolower();
        // loop to check palindrome
        for (int i = 0; i < n; i  , n--)
            if (word[i] != word[n - 1])
                return false;
        return true;
    }
    // function to find longest
    // palindrome word
    static string longestpalin(string str)
    {
        // to check last word for palindrome
        str = str   " ";
        // to store each word
        string longestword = "", word = "";
        int length, length1 = 0;
        for (int i = 0; i < str.length; i  )
        {
            char ch = str[i];
            // extracting each word
            if (ch != ' ')
                word = word   ch;
            else {
                length = word.length;
                if (checkpalin(word) &&
                        length > length1)
                {
                    length1 = length;
                    longestword = word;
                }
                word = "";
            }
        }
        return longestword;
    }
    // driver code
    public static void main()
    {
        string s = "my name is ava and i"
             " love geeksforgeeks";
        if (longestpalin(s) == "")
            console.write("no palindrome word");
        else
            console.write(longestpalin(s));
    }
}
// this code is contributed by manish
// shaw (manishshaw1)

java 描述语言


output: 

ava

方法二:使用 python 中的 方法:

  • 其思路是将 的单词串成一个列表。
  • 遍历列表并将所有回文单词追加到新列表中
  • 使用 sorted()方法按单词长度的递增顺序对新列表进行排序。
  • 最后,打印列表中的最后一个字符串。

下面是上述方法的实现。:

python 3

# python3 program for the above approach
def ispalindrome(string):
    if(string == string[::-1]):
        return true
    else:
        return false
def largestpalin(s):
    # taking new list
    newlist = []
    # traverse the list
    for i in s:
        if(ispalindrome(i)):
            newlist.append(i)
    # using sorted() method
    s = sorted(newlist, key=len)
    # print last word
    print(s[len(s)-1])
# driver code
if __name__ == "__main__":
    # given string
    s = "my name is ava and i love geeksforgeeks"
    # convert string to list
    l = list(s.split(" "))
    largestpalin(l)
# this code is contributed by vikkycirus

输出:

ava