This has been a long evening, I am trying to get this code to work all night, and all yesterday, too.
Anyway this is what I have:
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define STSIZE 30
void reverse(char *s, int first, int last);
void swap(char *p, char *q);
int maybepalind(char opalinds[], char palinds[]);
int main (void)
{
/*character array declarations*/
char palind[STSIZE];
char palinds[STSIZE];
char opalinds[STSIZE];
/*variable declarations*/
int i = 0;
int j = 0;
int k = 0;
int palans = 0;
/*banner and prompt for user*/
printf("Enter a string of characters.");
printf("\nThe computer will determine if the string is a palindrome");
printf("\n(spelled the same frontwards and backwards).");
printf("\nEnter your string of characters for evaluation: > ");
gets(palind);
printf("%s\n", palind);
int n = strlen(palind);
/*makes all upper case letters lower case*/
for( i = 0; i < n; ++i)
if(isupper(palind[i]))
palind[i] = tolower(palind[i]);
/*removes all spaces and punctuation*/
while(palind[k] != '\0' && k < n)
{
if(palind[k] != ' ' && !ispunct(palind[k]))
{
palinds[j] = palind[k];
++j;
}
++k;
}
palinds[j] = '\0'; /*terminates new character array*/
printf("%s\n", palinds);
/*copies array to new array for comparison in function below*/
char strcpy(char opalinds, char palinds);
/*function call to reverse letters in character array for comparison*/
reverse(palinds, 0, n);
/*function call to actually do the palindrome comparison on original and reverse string*/
/*sets palins equal to the value returned by function*/
palans = maybepalind(opalinds, palinds);
/*if function maybepalind returns '1' the string is a palindrome and that is printed*/
/*if the funtions returns a '0' the string is NOT a palindrome and that is printed.*/
if (palans = 1)
printf("\nThe character string : %s is a PALINDROME!\n", palind);
else
printf("\nThe character string : %s is NOT a PALINDROME!\n", palind);
}
/*function to reverse the characters in the string*/
void reverse(char *s, int first, int last)
{
if (first < last)
{
swap(&s[first], &s[last]);
reverse(s, ++first, --last);
}
}
/*the swap function called by the reverse function*/
void swap(char *p, char *q)
{
char tmp;
tmp = *p;
*p = *q;
*q = tmp;
}
/*the palindrome check function*/
int maybepalind(char opalinds[], char palinds[])
{
int first = 0;
int ans = 0;
if(opalinds[first] = '\0')
ans = 1;
else if (opalinds[first] != palinds[first])
{
printf("%c %c\n", opalinds[first], palinds[first]);
ans = 0;
}
else
maybepalind(opalinds[first + 1], palinds[first + 1]);
return (ans);
}
I get an error that says that the very last line:
maybepalins(opalinds[first + 1], palinds[first + 1]);
cannot convert parameter 1 from char to char[].
I am not sure what that means. I have tried every change I can think of.
If anyone has any ideas I would appreciate the input. Staring at this code so long I think I am going crosseyed.
Once I can get it to compile I think it is returning 1 all the time, because the string, no matter what I type in is always a palindrome.
Thanks Everyone!!!