populating a string only alpha characters
I am a C student (intermediate i think). I am having difficulties populating a string with only alpha characters. I have tried using a combination of gets() and isalpha() with no success. If this method is possible then I cant seem to nail down the logic. I have tried using isalpha(*str) in main and in the function definition itself with no success. Can someone point me in the right direction?
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define _CRT_SECURE_NO_DEPRECATE
#define MAX 61
int isPalindrome(char* str);
void printResults(char* str, int answer);
int main()
{
char str[MAX];
int answer;
puts("Enter a string");
gets(str);
answer = isPalindrome(str);
printResults(str, answer);
}
/*************************** FUNCTION ***************************
* isPalindrome()
* This takes only the alpha characters of a given string,
* converts them into all upper case and returns 1 if
* the string is a palindrome
*************************** FUNCTION ***************************/
int isPalindrome(char* str)
{
char* end = str + strlen(str);
int i;
for (i = 0; i < strlen(str); i++)
{
str[i] = toupper(str[i]);
}
while (end > str)
{
if (*str++ != *--end)
{
return 0;
}
}
return 1;
}
/*************************** FUNCTION ***************************
* printResults()
* This string prints the string entered by the user and the
* awnswer to whether or not it is a palindrome
*************************** FUNCTION ***************************/
void printResults(char* str, int answer)
{
if (answer == 1)
{
printf("%s is a palindrome\n", str);
}
else
{
printf("%s is NOT a palindrome\n", str);
}
}