This is a homework assignment, but it's already done and it's not for a grade; it's meant as a refresher for the rest of the course. I've never done anything with dynamic memory allocation in C before, so I'm assuming I've done at least something wrong.
The goal is to write a program that takes a string from a user and checks to see if it's a palindrome. All non-letter characters are ignored, and dynamic memory allocation is required.
It works fine and I'm pretty happy with it, but I have no way of knowing if I've done things the "right" or "wrong" way. So, could anyone look over it really quickly and point out all the stupid things I've done?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
//str holds raw input
//str_san holds sanitized input
char *str = (char *)malloc(50), *str_san;
//i and k are the index counters
//len is the length of the sanatized string
int i = 0, k = 0, len;
//get a string from the user
printf("Enter a message: \n");
//store it in str
scanf("%s", str);
//make str_san as small as possible
str_san = (char*)malloc(strlen(str) + 1);
//copy only letters into the sanitized string
while (str[i] != '\0') {
if (str[i] >= 'A' && str[i] <= 'Z') {
str_san[k] = str[i] + 32; //changes uppercase to lowercase
k++;
}
if (str[i] >= 'a' && str[i] <= 'z') {
str_san[k] = str[i];
k++;
}
i++;
}
//str is no longer needed
free(str);
//terminate sanitized string
str_san[k] = '\0';
//set len to the length of str_san
len = strlen(str_san);
//check for palindrominess
for (i = 0; i < len; i++) {
//if the first letter does not equal the last letter then it's not a palindrome
if (str_san[i] != str_san[len - i - 1]) {
printf("Not a palindrome");
return 0;
}
}
//it is now safe to free str_san
free(str_san);
//if we've made it this far, it's a palindrome
printf("Palindrome");
return 0;
}