Doing an assignment where I have to implement some functions. One of those functions is to check if a string is a palindrome.
#include <stdio.h>
#include <stdlib.h>
char string[] = "cameron";
char string2[] = "mah";
char palindrome[] = "madam";
char notPalindrome[] = "music";
int removeChar(char *str1, char * str2, char c){
int length = 0;
for (int i = 0; str1[i] != '\0'; i++){
length++;
}
for (int i = 0; i <= length; i++){
if (str1[i] == c){
str2[i] = '*';
}
else {
str2[i] = str1[i];
}
}
for (int i = 0; i<= length; i++){
printf("%c", str2[i]);
}
}
int isPalindrome(char *str){
int length = 0;
for (int i = 0; str[i] != '\0'; i++){
length++;
}
int j = length - 1;
int reversible = 0;
for (int i = 0; i < j; i++){
if (str[i] != str[j]){
reversible++;
break;
}
j--;
}
if (reversible > 0){
printf("\nString is not a palindrome\n");
}
else {
printf("\nString is replaceable\n");
}
}
int main(){
removeChar(string, string2, 'm');
isPalindrome(palindrome);
return 0;
}
When I run this code it says the string is not a palindrome when it should be, but why is it that if I change isPalindrome(palindrome); to isPalindrome("madam"); it works.
Also Why is it that if I comment out //removeChar(string, string2, 'm');, isPalindrome() will work properly.