For a school assignment i need to create a program that lets the user input a string that should be stored in an array. Then the program calls a function that removes all the spaces in the array. After that call on a function that makes all letters lowercase. After that call a 3rd function that checks if the current array is a palindrome and return 1 if it is and 0 if not. The program works fine. But after i put a do while around the code that lets you input the array and call the functions it seems to skip the scanf that takes the input into an array, basically the program does not let me input another string to be stored in the array to check if that is a palindrome. Currently i have deleted the do while in my code but i have to have it implemented, can someone help me?
int is_palindrome(char input_string[]);
void delete_spaces(char input_string[]);
void convert_to_lower(char input_string[]);
int main(void)
{
char str[1000];
char again;
do {
printf("Mata in en sträng: ");
int i = 0;
for (i = 0; i < 100; i++)
{
scanf("%c", &str[i]);
if(str[i]=='\n')
{
break;
}
}
delete_spaces(str);
convert_to_lower(str);
int palindrom_or_nah = is_palindrome(str);
if(palindrom_or_nah == 1)
printf("\nSträngen är ett palindrom\n\n");
else
printf("\nSträngen är inte ett palindrom\n\n");
printf("Vill du köra igen? j/n: ");
scanf(" %c", &again);
}while(again == 'j');
return 0;
}
//Kolla ifall strängen är ett palindrom
int is_palindrome(char input_string[])
{
int answer = 0, i = 0;
int h = (unsigned)strlen(input_string)-1;
while(h>1)
{
if(input_string[i++] != input_string[h--])
{
break;
}
else
{
answer = 1;
}
}
return answer;
}
//Ta bort mellanslag och andra tecken i strängen
void delete_spaces(char input_string[])
{
int i = 0, j = 0;
char rem_space[strlen(input_string)];
while(input_string[i] != '\0')
{
if(input_string[i] != ' ')
{
if(isalpha(input_string[i]))
{
rem_space[j++] = input_string[i];
}
}
i++;
}
rem_space[j] = '\0';
for(i = 0; i < strlen(input_string); i++)
{
input_string[i] = rem_space[i];
}
}
//Gör alla bokstäver till små bokstäver
void convert_to_lower(char input_string[])
{
int count = (unsigned)strlen(input_string), i;
for (i = 0; i < count; i++)
{
input_string[i] = tolower(input_string[i]);
}
}