'isPalindrome homework exercise

Write a program that uses the function isPalindrome given in Example 6-6 (Palindrome). Test your program on the following strings:

madam, abba, 22, 67876, 444244, trymeuemyrt

Modify the function isPalindrome of Example 6-6 so that when determining whether a string is a palindrome, cases are ignored, that is, uppercase and lowercase letters are considered the same.

The isPalindrome function from Example 6-6 has been included below for your convenience.

bool isPalindrome(string str) 
{
int length = str.length(); 
for (int i = 0; i < length / 2; i++) {
    if (str[i] != str[length – 1 – i]) {
         return false;
    } // if    
  } // for loop
 return true;
}// isPalindrome
Your program should print a message indicating if a string is a palindrome:

madam is a palindrome

My program so far is this

#include <iostream>
#include <string.h>
using namespace std;

int main () {

bool isPalindrome (string str);
string str;
int length = str.length(); 

cout << "Enter a string: ";
getline (cin,str);

for (int i = 0; i < length / 2; i++) {
    if (str[i] != str[length -1 -i]) {
         cout << str << "Is not a Palindrome";
         return false;
    } else if (str[i] == str[length -1 -i] && toupper(str[i]) != islower(str[i])) {
        cout << str << "Is a Palindrome";
  } // for loop
return true;
}

}

I do not know what im doing wrong I sent everything to make sure it matches the word backwards and then when it is true it will return true. I am very to new to programming and I am sorry if my code is a little sloppy.

c++


Solution 1:[1]

This is a modification of your code. It wasn't too logical that you were declaring the function inside so i just put it outside.

#include <iostream>
#include <string.h>
using namespace std;

bool isPalindrome(string str) {
    int length = str.length();

    for (int i = 0; i < length / 2; i++) {
        if (str[i] != str[length -1 -i]) {
            cout << str << "Is not a Palindrome";
            return false;
        } else if (str[i] == str[length -1 -i] && toupper(str[i]) != islower(str[i])) {
                cout << str << "Is a Palindrome";
        } // for loop
        return true;
    }
    return false;
}

int main () {

    string str;

    cout << "Enter a string: ";
    getline (cin,str);
    isPalindrome(str);
}

Solution 2:[2]

    public static bool IsPalindrome(string value)
    {
        int i = 0;
        int j = value.Length - 1;
        while (true)
        {
            if (i > j)
            {
                return true;
            }
            char a = value[i];
            char b = value[j];
            if (char.ToLower(a) != char.ToLower(b))
            {
                return false;
            }
            i++;
            j--;
        }
    }

Solution 3:[3]

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 1000


bool isPalindrome(int x){
  int c[MAX];
  int i = 0;
  int j;
  int k = 0;
  bool z;
  if(x < 0){
      return false;
  }
  while (x != 0){
    int r = x % 10;
    c[i] = r;
    i++;
    x = x / 10;
    }
      for (j = i - 1; j > -1; j--) {
      printf("%d ", c[j]);
    }  
  for(k = 0; k <= (i / 2); k++){
    if(c[k] == c[i - k - 1]){
        z = true;
    }
else
    {
      z = false;
    }
  }
    return z;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luis Liz
Solution 2 Yusuf Ünlü
Solution 3 Jazzn_ Bass