Question

To write a C + + function and algorithm to accept a string and check if...

To write a C + + function and algorithm to accept a string and check if the given string is palindrome or not.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

bool isPalindrome(string a)
{
// declaring variables
int first = 0, last = a.length() - 1;
  
while(first < last) // checking till middle
{
// if the characters are not matching, returning false
if(a[first++] != a[last--])
return false;
}
return true;
}

int main() {
cout << boolalpha << isPalindrome("racecar") << endl;
cout << isPalindrome("HomeworkLib") << endl;
}

// -- Please up vote or comment if you have any doubts. Happy Learning!

Add a comment
Know the answer?
Add Answer to:
To write a C + + function and algorithm to accept a string and check if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C language: Write a program that uses a function to check if a user entered string...

    C language: Write a program that uses a function to check if a user entered string is a palindrome. Based on the return value of the function, the results are printed in the main() function. (do not print results in function to check for palindrome) A palindrome reads the same forward as backward for example tacocat or civic.

  • Write a C++ program to check whether a string is a palindrome. Input a string (word)...

    Write a C++ program to check whether a string is a palindrome. Input a string (word) from the keyboard. Assume the letters are case-insenstive. Use the C++ string class. Refer to the sample output below. Sample Runs: Enter a string: otto; otto is a palindrome Enter a string: A A is a palindrome Enter a string: heooh heooh is not a palindrome

  • Write a function check palindrome, which takes a string x as argument and which returns True...

    Write a function check palindrome, which takes a string x as argument and which returns True if x is a palindrome, and False otherwise. A palindrome is a word that reads the same backwards as forwards (like for example “racecar”). Your function should be recursive (i.e. call itself) and proceed as follows. 1. If x is a string of length 0 or 1 return True. 2. If the rst and the last letter of x are di erent, return False....

  • You need to write a program (one java class containing Main calling function isPalindrome (String str)....

    You need to write a program (one java class containing Main calling function isPalindrome (String str). The function isPalindrome (returns Boolean T/F) needs to determine whether or not a string is a palindrome, using recursion. The algorithm to check whether a string is a palindrome is shown below: /* check for first and last char of String: * if they are same then do the same thing for a substring * with first and last char removed. and carry on...

  • Write a Python function that uses the stack data structure to check if a string is...

    Write a Python function that uses the stack data structure to check if a string is a palindrome or not. You can use any of the dollowing stack methods: push(), pop(), peek, empty(). Note that a palindrome is a word, phrase, or sequence that reads the same backwards as forward

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • Write a C function that receives a character array (string), and it will check the characters...

    Write a C function that receives a character array (string), and it will check the characters of the array to figure out whether the string has a given valid pattern. The required pattern will be given to you. In this function, for instance, you should check if the ascii code of an element of the character array is between a range. The ascii table of all the printable characters will be provided to you. See the last page of this...

  • Please i need programs in C 32) Write a function that, given a string, a width,...

    Please i need programs in C 32) Write a function that, given a string, a width, and an empty string for output, centers the string in the output, centers the string in the output area. The function is to return 1 if the formating is successful and 0 if any errors, such as string length greater than width, are formed. 35) Write a function called newStrCmp that does the same job as strcmp. The declaration for your functions is to...

  • ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring...

    ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. int indexOf(const string& s1, const string& s2) Write a test program that reads two strings and checks whether the first string is a substring of the second string.

  • palindrome is a string that reads the same both forward and backward. C++ For example, the...

    palindrome is a string that reads the same both forward and backward. C++ For example, the string "madam" is a palindrome. Write a program that uses a recursive function to check whether a string is a palindrome. Your program must contain a value-returning recursive function that returns true if the string is a palindrome and false otherwise. Do not use any global variables; use the appropriate parameters.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT