Hey guys!
I am working on a palindrome assignment and I have pretty much everything figured out up until the end. Here is my code so far:
Code:
#include <stdio.h>#define TRUE 1
#define FALSE 0
void make_copy_of_string(char str[], char str_copy[]);
void keep_chars(char string[]);
void convert_uper_to_lower_case(char string[]);
_Bool palindromness(char string[]);
int main(void)
{
char phrase[101]; //create array to hold input
char phrase_copy[101]; //array that holds copy of input which will be modified
printf("Enter a phrase: ");
fgets(phrase, 101, stdin); //stores input into 'phrase' with a max 100 chars
printf("%s\n", phrase); //tests if fgets is working
make_copy_of_string(phrase, phrase_copy); //makes a copy of phrase
printf("%s\n\n", phrase_copy);
keep_chars(phrase_copy);
printf("%s\n", phrase_copy);
convert_uper_to_lower_case(phrase_copy);
printf("%s\n", phrase_copy);
return 0;
}
void make_copy_of_string(char str[], char str_copy[])
{
int i = 0; //to keep track of which char is being copied
while(str[i] != '\n' && str[i] != '\0')
{
str_copy[i] = str[i];
i++;
}
str_copy[i] = '\0';
str[i] = '\0';
}
void keep_chars(char string[])
{
int i=0, j=0;
while (string[i] != '\0')
{
if ( ('A'<= string[i] && string[i] <= 'Z') || ('a' <= string[i] && string[i] <= 'z'))
{
string[j] = string[i];
i++;
j++;
}
else
i++;
}
string [j] = '\0';
}
void convert_uper_to_lower_case(char string[])
{
int i = 0;
while (string[i] != '\0')
{
if ('A' <= string[i] && string[i] <= 'Z')
{
string[i] += 32;
i++;
}
else
i++;
}
}
_Bool palindromeness(char string[])
{
}
The only issue I am running in, is figuring out how to set up the _Bool palindromeness function. I was thinking of perhaps making a copy of "convert_uper_to_lower_case" and doing an if loop to see if the first char matched the last char of the copy.
With this setup I described, I am still not sure how to set it up really, and I would rather work on a better method. I am only allowed to use stdio.h as well.