answersLogoWhite

0


Best Answer

forward(word)

backward(word)

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program that allows the user to input a word or phrase and then determine if its a palindrome. I need to use a Boolean equals valued Function procedure names IsPalindrome. How?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Using Python write an algorithm that takes a string as input and determines whether or not it is a palindrome?

def isPalindrome(s): return s == s[::-1] then just call the function with a string like isPalindrome('poop')


Javascript code for palindrome?

AnswerFirst find the end of the string.Then compare the first character to the last character, and if they are different the string is not a palindrome.Then go the next pair of characters and do # 2 until you reach the middleHere's a quick and dirty example:int ispalindrome(const char *s){const char *end = s;while (*++end);while (end > s)if (*--end != *s++) return 0;return 1;}This example doesn't skip punctuation or spaces, and it compares letters case sensitively. You are more than welcome to use and improve upon this example.


C plus plus program of a palindrome?

There are several ways to determine if a string is a palindrome or not. Typically, you must first ignore all spacing, capitalisation and punctutation within the string, which can be done by copying the string, character by character, ignoring all non-alphanumeric characters. You then point to the first and last characters in the modified string and, so long as both characters under the pointers are the same, work the pointers towards the middle of the string until the pointers either meet in the middle or they pass each other. That is, if the string has an odd count of characters, the pointers will meet at the middle character, otherwise they will pass each other. At that point you can say with certainty the string is a palindrome. If the characters under the pointers differ at any time, then the string is not a palindrome. This is fairly straightforward to program. A more interesting problem is when you have to locate the longest palindrome within a string which is not itself a palindrome. For instance, the string "Madam, I'm Adam is a palindrome" is not a palindrome, but it does contain one: "Madam I'm Adam". In this case we cannot point to the first and last characters and work towards the middle. Instead, we have to test every possible substring of the string. We do this by starting at the first character and treat it as if it were actually the middle character of a palindrome, and then move our pointers to the left and right of this character while the characters match. When they no longer match, or one of the pointers has reached either end of the string, we store the longest palindrome found up to that point and then move onto the next character and treat it as the middle character. If we continue in this manner, treating every character as if it were the middle character of a palindrome, we will eventually locate the longest palindrome. The problem with this approach is when the longest palindrome has an even number of characters instead of an odd number. To get around this we simply place a single space between each character, and treat each of those as being the middle character as well. When a palindrome is found, we simply remove the spaces. In this way we can use exactly the same algorithm to cater for both odd and even character palindromes. The only remaining problem is when we wish to print the palindrome itself. Since this will be a substring of the original string, we cannot use the modified string we used to locate the palindrome. One way to get around that is to store the original positions of each letter in an array of indices, and use that array to determine where the substring lies with in the original string. The following program demonstrates this technique in full. The key function is the ispalindrome() function, which accepts a lower-case copy of the string (including the original spacing an punctuation), and a vector that contains the indices of each letter within the string (ignoring puctuation and spacing), separated by -1 values (representing the implied spaces between each letter). The pos value tells the function which index of the vector is to be treated as the middle character of the potential palindrome, while x and y are output parameters that determine the start and end of the palindrome within the vector. The function returns true if a palindrome was found, and the x and y values can be used to extract the palindrome from the original string, using the indices stored in the vector. Note that when the search for a palindrome fails, we step back the x and y indices by one, and if the vector index is -1, then we step back another index. We then test the x and y values to see if they indicate a palindrome was found or not. The strip() function is another key function. This generates the vector from the lower case copy of the original string. Although we could eliminate the -1 values at the start and end of the vector, it's simpler to just leave them in. You will note that the program can cater for strings that are themselves palindromes, as well as strings that contain palindromes. #include<iostream> #include<string> #include<vector> using namespace std; string input_string(string prompt) { cout<<prompt<<":\t"; string input; getline(cin, input, '\n'); return(input); } void convert_tolower(string& s) { for(string::iterator i=s.begin(); i!=s.end(); ++i) *i=tolower(*i); } vector<int> strip(const string& s) { vector<int> v; v.push_back(-1); for(int i=0; i<s.size(); ++i) { if((s[i]>='a' && s[i]<='z') (s[i]>='0' && s[i]<='9')) { v.push_back(i); v.push_back(-1); } } return(v); } bool ispalindrome(const string s, const vector<int> v, int pos, int& x, int& y) { for(x=pos,y=pos; x>=0 && y<v.size(); --x, ++y) if( v[x]!=-1 && ( s[v[x]]!=s[v[y]] )) break; ++x, --y; if( v[x]==-1 ) ++x, --y; return(x>=0 && x<y && y-x>1); } int main() { string input; while(1) { input=input_string("Enter a string"); if(input.size()==0) break; string copy(input); convert_tolower(copy); vector<int> v=strip(copy); string pal; int pos=0; for(int i=0; i<v.size(); ++i) { int start=0, end=0; if( ispalindrome( copy, v, i, start, end)) { string tmp( input.substr(v[start],v[end]-v[start]+1)); if( tmp.size() > pal.size() ) { pal = tmp; pos = v[start]; } } } if( pal.size() ) { cout<<"Palindrome:\t"; for(int i=0; i<pos; ++i) cout<<" "; cout<<pal<<"\n"<<endl; } else cout<<"The string contains no palindromes!\n"<<endl; } return(0); } Example output: Enter a string: Madam, I'm Adam Palindrome: Madam, I'm Adam Enter a string: Madam, I'm Adam is a palindrome Palindrome: Madam, I'm Adam Enter a string: In girum imus nocte et consumimur igni Palindrome: In girum imus nocte et consumimur igni Enter a string: 0123456765432 Palindrome: 23456765432 Enter a string: Press any key to continue . . .


What statements indicate the start and end of a procedure?

A procedure is started by calling the function that represents that procedure. The function call must include any and all required arguments.The procedure ends whenever a return statement is encountered anywhere within the function body, or execution falls off the end of the function (assuming no return value is expected from the procedure), or a non-return function is invoked by the function (such as the abort() function) or an unhandled exception is thrown by the function. Apart from a non-returning function call, execution always returns to the calling code (the caller). If an unhandled exception is thrown by a function, the call stack automatically "unwinds" until a suitable exception handler is found. If no handler is found on the call stack, the global main function will unwind, terminating the program with an unhandled exception error. Hence the reason all non-trivial programs should provide a "catch-all" exception handler in the global main function.


What is the purpose of a function or procedure?

function is a set of statements that can be executed in the part of the program. ex: to add two nos. using function void main() { int a,b,c; printf("enter the two numbers"); scanf("%d%d",&a,&b); add(a,b); clear(); } void add(int a,int b) { c=a+b; printf("the sum is %d",c); }

Related questions

Using Python write an algorithm that takes a string as input and determines whether or not it is a palindrome?

def isPalindrome(s): return s == s[::-1] then just call the function with a string like isPalindrome('poop')


Program for palindrome in php?

You could use a function like this:function isPalindrome($string) {$string = strtolower($string);return (strrev($string) == $string) ? true : false;}and then to check a palindrome call an if statement like so:if(isPalindrome($test)) {echo $test.' is a palindrome';}else {echo $test.' is not a palindrome';}


How can a procedure be defined in C plus plus?

A procedure is simply a function in C++, therefore you define procedures just as you would any function. In some languages, a procedure is not a function as such, insofar as there is no return type. The C++ equivalent would therefore be a function that returns void.


Javascript code for palindrome?

AnswerFirst find the end of the string.Then compare the first character to the last character, and if they are different the string is not a palindrome.Then go the next pair of characters and do # 2 until you reach the middleHere's a quick and dirty example:int ispalindrome(const char *s){const char *end = s;while (*++end);while (end > s)if (*--end != *s++) return 0;return 1;}This example doesn't skip punctuation or spaces, and it compares letters case sensitively. You are more than welcome to use and improve upon this example.


The function of the cell is determined by?

structureThe shape of the protein will determine the cell. It will also determine the function of the cell.


What is the difference between a function and Procedure?

Girraf


Procedure to replace kidney function'?

dialysis


What does structure determine?

Function


The function of a cell is determined by its?

structureThe shape of the protein will determine the cell. It will also determine the function of the cell.


How is a procedure identified as near or far?

The far procedure is used at the place where the function call is given in main program and function definition is given in sub program....


What do you call a variable of a function that is active only within the procedure or its function?

local variable


What is the code for Palindrome through for loop in c plus plus?

There are several ways to determine if a string is a palindrome or not. Typically, you must first ignore all spacing, capitalisation and punctutation within the string, which can be done by copying the string, character by character, ignoring all non-alphanumeric characters. You then point to the first and last characters in the modified string and, so long as both characters under the pointers are the same, work the pointers towards the middle of the string until the pointers either meet in the middle or they pass each other. That is, if the string has an odd count of characters, the pointers will meet at the middle character, otherwise they will pass each other. At that point you can say with certainty the string is a palindrome. If the characters under the pointers differ at any time, then the string is not a palindrome. This is fairly straightforward to program. A more interesting problem is when you have to locate the longest palindrome within a string which is not itself a palindrome. For instance, the string "Madam, I'm Adam is a palindrome" is not a palindrome, but it does contain one: "Madam I'm Adam". In this case we cannot point to the first and last characters and work towards the middle. Instead, we have to test every possible substring of the string. We do this by starting at the first character and treat it as if it were actually the middle character of a palindrome, and then move our pointers to the left and right of this character while the characters match. When they no longer match, or one of the pointers has reached either end of the string, we store the longest palindrome found up to that point and then move onto the next character and treat it as the middle character. If we continue in this manner, treating every character as if it were the middle character of a palindrome, we will eventually locate the longest palindrome. The problem with this approach is when the longest palindrome has an even number of characters instead of an odd number. To get around this we simply place a single space between each character, and treat each of those as being the middle character as well. When a palindrome is found, we simply remove the spaces. In this way we can use exactly the same algorithm to cater for both odd and even character palindromes. The only remaining problem is when we wish to print the palindrome itself. Since this will be a substring of the original string, we cannot use the modified string we used to locate the palindrome. One way to get around that is to store the original positions of each letter in an array of indices, and use that array to determine where the substring lies with in the original string. The following program demonstrates this technique in full. The key function is the ispalindrome() function, which accepts a lower-case copy of the string (including the original spacing an punctuation), and a vector that contains the indices of each letter within the string (ignoring puctuation and spacing), separated by -1 values (representing the implied spaces between each letter). The pos value tells the function which index of the vector is to be treated as the middle character of the potential palindrome, while x and y are output parameters that determine the start and end of the palindrome within the vector. The function returns true if a palindrome was found, and the x and y values can be used to extract the palindrome from the original string, using the indices stored in the vector. Note that when the search for a palindrome fails, we step back the x and y indices by one, and if the vector index is -1, then we step back another index. We then test the x and y values to see if they indicate a palindrome was found or not. The strip() function is another key function. This generates the vector from the lower case copy of the original string. Although we could eliminate the -1 values at the start and end of the vector, it's simpler to just leave them in. You will note that the program can cater for strings that are themselves palindromes, as well as strings that contain palindromes. #include<iostream> #include<string> #include<vector> using namespace std; string input_string(string prompt) { cout<<prompt<<":\t"; string input; getline(cin, input, '\n'); return(input); } void convert_tolower(string& s) { for(string::iterator i=s.begin(); i!=s.end(); ++i) *i=tolower(*i); } vector<int> strip(const string& s) { vector<int> v; v.push_back(-1); for(int i=0; i<s.size(); ++i) { if((s[i]>='a' && s[i]<='z') (s[i]>='0' && s[i]<='9')) { v.push_back(i); v.push_back(-1); } } return(v); } bool ispalindrome(const string s, const vector<int> v, int pos, int& x, int& y) { for(x=pos,y=pos; x>=0 && y<v.size(); --x, ++y) if( v[x]!=-1 && ( s[v[x]]!=s[v[y]] )) break; ++x, --y; if( v[x]==-1 ) ++x, --y; return(x>=0 && x<y && y-x>1); } int main() { string input; while(1) { input=input_string("Enter a string"); if(input.size()==0) break; string copy(input); convert_tolower(copy); vector<int> v=strip(copy); string pal; int pos=0; for(int i=0; i<v.size(); ++i) { int start=0, end=0; if( ispalindrome( copy, v, i, start, end)) { string tmp( input.substr(v[start],v[end]-v[start]+1)); if( tmp.size() > pal.size() ) { pal = tmp; pos = v[start]; } } } if( pal.size() ) { cout<<"Palindrome:\t"; for(int i=0; i<pos; ++i) cout<<" "; cout<<pal<<"\n"<<endl; } else cout<<"The string contains no palindromes!\n"<<endl; } return(0); } Example output: Enter a string: Madam, I'm Adam Palindrome: Madam, I'm Adam Enter a string: Madam, I'm Adam is a palindrome Palindrome: Madam, I'm Adam Enter a string: In girum imus nocte et consumimur igni Palindrome: In girum imus nocte et consumimur igni Enter a string: 0123456765432 Palindrome: 23456765432 Enter a string: Press any key to continue . . .