Hi guys. so I have been trying to solve this problem and I wrote a bit of which I think its part of the solution. so I would apreciate a help from you guys.

The problem:
A palindrome is a word, phrase, or any other sequence of letters that can be spelled the same way in either direction. For example, “level” and “noon” are palindromes. Write a recursive logical-function that returns 1 if the word is a palindrome and 0 otherwise. You may do it by matching the first letter with the last, the second with the next-to-last, and so on. Matching letters should be non-case- sensitive that is upper case letters match lower case letters. Notice that a single letter is not considered a palindrome.
Write a driver (main) function to prompt the user to enter a sentence. Use your recursive logical- function to count the number of palindromes in that sentence. You need to use String Tokenization (strtok) function to split the sentence into words.

Recommended Answers

All 9 Replies

OK, just post your code and example data, so we can see how it looks to us.

this is just an attempt to write a part of the function required.

int myfunction (char s[ ], size){
int r, n;
for(n=0; n>0; n++){
if(tolower(s[n])== tolower(s[strlen(s)-n-1])&& n!=(int)(strlen(s)/2))
continue;
else if(tolower(s[n])== tolower(s[strlen(s)-n-1]))
r=1;
else
r=0;}
return r;

Sample output of the program is in the attachments.

12221

This post has no text-based content.

Without running code it looks wrong because you should return immediately if pair does not match.

Write a recursive logical-function

This it is not, it is not recursive.

I am a bit weak in functions writing. can anyone help me please?

So far you've only given vague information. You need to be specific in what you need help with and post code that shows what you are having trouble with. The more detailed the information, the more good help you will receive.

So far, our best response to

I am a bit weak in functions writing. can anyone help me please?

is "yes". Nothing more,

what is function's parameter (should the size of the string be one of them)?
what are steps to write a recursive function in such a case?
how the function "strtok" works? and how it could be useful to solve this problem?
would it be easy to write this program without using recursive function?

You normally would think what are those fundamental cases which you know trivially to separate to palindrome and not palindrome. That depends on the definition for the task that one letter is not palindrome. It means empty or one letter case are not good base cases. Of course you must then find the way to do inductive step from n+1 or n+2 long case to n long case.

I did the recursive function in Python language, which I know better and is higher level and the definition took 3 lines plus one line for variable to avoid recalculation of one needed value. Calling in print statement and header of function definition brought the code to 6 LOC. But with C++ you need to be little more verbose. Actually now I checked little the function and the function is possible to give as single return statement (and the optional variable assignment for efficiency is also not really needed).

#include<iostream>
#include<string>
using namespace std; // the namespace for cout<< & such functions
int main()
{
    char strn[80];
    cout<<"Enter the string: ";
    cin.getline(strn,80);
    int len=strlen(strn);

    bool flag=true; // create a Boolean value, "flag" to be used in our loop

    for (int c=0; c!=len/2; c++) // do a loop from 0 to half the length of the string
    {
        if (flag) // if it is a palindrome so far
        {
            if (strn[c]!=strn[len-c-1]) // check the characters match
            {
                flag=false; // if they don't set the indicator to false
            }

        }
        else
        {
            break; // if it is not a palindrome, exit the for loop
        }
    }

// if flag is true cout "Palindrome" otherwise output "Not Palindrome"

    if (flag)
    {
        cout<<"Palindrome";
    }
    else
    {
        cout<<"Not Palindrome";
    }

    cin.get();
    return 0;
}

----------
This should do it.

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.