1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ikadub [295]
3 years ago
6

Write a bool function isPalindrome that takes one string parameter and returns true if that string is a palindrome and false oth

erwise. c++

Computers and Technology
1 answer:
kkurt [141]3 years ago
7 0

Answer:

Here is the bool function isPalindrome:

bool isPalindrome(string s)

{  bool palindrome;      

 int j;

 int len = s.length();

 for (int i =0, j= len-1; i < len/2; i++, j--)  

   if (s[i] == s[j])

     palindrome = true;  

   else

     palindrome = false;

 return palindrome; }

Explanation:

The bool function isPalindrome takes a string as parameter and returns true if the string is a palindrome or false if the string is not palindrome. The string variable s holds the string value. In function isPalindrome, there is a bool type variable palindrome. bool is a data type which is used to hold values true or false which is then stored in the palindrome variable.

Next it is being found that how many characters are there in the input string using length() function which returns the length of the string. The length of the string is stored in int type variable len.

Next the for loop starts which has two variables i and j where i starts at 0 position of the input string and j starts at the last position of the string. These two variables move or traverse along the string this way. i variable stops when half the length of the input string is reached. At the end of every iteration i moves forward by the increment of its value to 1 and j moves backwards by the decrement  of its value to 1.

Every the body of  the loop executes, there is an if condition in the body of the loop which checks if the string is a palindrome. This is checked by matching the character at position/index i with the character at position j of the string. This will continue character by character until the i reached half the length of the input string and if every character in i was matched to every character in j then palindrome = true; statement is executed which returns true as the string is a palindrome otherwise palindrome = false; is returned as the string is not a palindrome.

For taking input string from the user and to check if the function isPalindrome() works correctly a main() function is written which takes input string from the user and reads in variable str calls isPalindrome() function. If the string is palindrome then the cout statement in main() function prints the message: String is a palindrome and if the input string is not a palindrome the the following message is displayed in the output screen: String is not a palindrome

int main()

{

string str;

cout<<"Enter a string: ";

cin>>str;

if(isPalindrome(str))

cout<<"String is a palindrome";

else

cout<<"String is not a palindrome";

}

The screenshot of the program along with the output is attached.

You might be interested in
3. Which of the following is the best definition of a workplace policy?
Oksi-84 [34.3K]

The best definition of a workplace policy is: d. the standard course of action adopted by a business.

Workplace policies are a company’s standard rules of what should and should not be done by its workforce. These policies are strictly enforced as the workforce’s actions reflects the company. Failure to comply with these policies may lead to a member of the workforce’s removal from the business.

6 0
4 years ago
What is the function of print format
Ivahew [28]
Print formatted refers to a control parameter used by a class of functions in the string-processing libraries of various programming languages.
6 0
3 years ago
Read 2 more answers
In which of the following situations would you want to use GOMS/KLM as an evaluation tool?
Elan Coil [88]

Answer:

B. Amazon's create an account process

Explanation:

The Keystroke-Level Model (KLM) is a cognitive modeling tool which can be used in predicting the performances carried out by human on interface designs. It's the most "practical" of the GOMS methodologies.

This model tends to predict how long it will take for the completion of a routine task by an expert user. There should be no errors.

So, Option B is correct because GOMS/KLM is an evaluation tool which fits in the evaluation of Amazon's create an account process.

7 0
3 years ago
Write a Python program that gets a single character from the user and writes out a congratulatory message if the character is a
DochEvi [55]

Answer:

def vowel(a):  #function to detect whether the character is vowel or not.

   vow=['a','e','i','o','u'] #list of vowels.

   if a in vow:

       return True

   else:

       return False

       

character=str(input("Enter the character \n")) #taking input.

if vowel(character.lower()):#checking the character is vowel using the function vowel..

   print("Congratulations!!!!") #congratulating.

else:

   print("You lose better luck next time")#message.

Output:-

Enter the character  

a

Congratulations!!!!

Explanation:

I have created a function to check whether the given character is a vowel or not.

After that taking input from the user.

And checking that it is vowel or not.If it is vowel then printing the message.

8 0
3 years ago
One solution for communicating indentations and the distinction between code and text is the use of _____.
BartSMP [9]
A. text-to-speech is the answer,that is the disto
5 0
3 years ago
Other questions:
  • Why are video texts an example of multimedia? A. They use audio and visual elements together. B. They use one type of medium to
    13·2 answers
  • What is an example of constructive criticism for an employee who is shy during meetings?
    15·2 answers
  • You have four 2.5ʺ hard drives on hand and need a replacement drive for a desktop system. The documentation for the motherboard
    9·1 answer
  • Que es lo que me recomiendan estudiar? :v
    10·1 answer
  • Routing in Practice (30 points). For each of the statements below, decide which it is true or false, and briefly justify your an
    13·1 answer
  • The Internet raises the bargaining power of customers most effectively by: making information available to everyone. reducing ba
    15·1 answer
  • Adam is an Engineering student but has decided that he does not want to work in that field, but in the Manufacturing career clus
    11·2 answers
  • Which technology provides a solution to IPv4 address depletion by allowing multiple devices to share one public IP address
    13·1 answer
  • Since JavaScript is case sensitive, one of the variables below needs to be
    11·1 answer
  • 2. The Warren Commission investigated the assassination of President _______________________ in 1964.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!