I am trying to make a program that takes a user inputted string and checks to see if it is a palidrome. I got it wrote out, but there is one problem. First, the string is not displayed. When the function I made checks through a string, it always evaluates the statement to either true, that it is a palidrome, or false, that it is not a palidrome, when the opposite result was true. For example, "Madam Im Adam" always evalutes to false in my current code and in a different implementation, "Hello World!" always evaluated to true. What am I doing wrong?
Here is the program source:
Here is my custom header source:
And here is the function definition:
I have a feeling that my problem rests in the definition and the source for both the main program and the function definition. I tried to do a loop that checks different positions of an array instead of using two different arrays and the result is still the same.
Update: I have fixed the problem, but now there is a new one. I now get a segementation fault on the string "Madam Im Adam".
Here is the updated function definition:
Here is the program source:
Code:
#include <stdio.h>
#define Size 700
#include "stringcheck.h"
int main()
{
char line[Size];
int check;
printf("Enter a string: ");
fgets(line, Size, stdin);
check = stringcheck(line);
if (check == 0)
{
printf("\"%s\" is not a palindrome\n", line);
}
else
{
printf("\%s\" is a palindrome\n", line);
}
return 0;
}
Here is my custom header source:
Code:
int stringcheck(char string[]);
And here is the function definition:
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define false 0
#define true 1
int stringcheck(char string[])
{
int size = strlen(string);
int count = size -2, count2 = 0, count3 = size -1;
int display;
char reverse[size];
for (count2; count2 <= size -1; count2++)
{
reverse[count2] = string[count3];
count3--;
}
for (display = 0; display <= count; display++)
{
if (reverse[display] == string[count] | reverse[display] == toupper(string[count]) | reverse[display] == tolower(string[count]))
{
return true;
}
count--;
}
return false;
}
I have a feeling that my problem rests in the definition and the source for both the main program and the function definition. I tried to do a loop that checks different positions of an array instead of using two different arrays and the result is still the same.
Update: I have fixed the problem, but now there is a new one. I now get a segementation fault on the string "Madam Im Adam".
Here is the updated function definition:
Code:
#include <stdio.h>
#include <string.h>
#define false 0
#define true 1
int stringcheck(char string[])
{
int size = strlen(string);
int count = size -1, count2 = 0;
char reverse[size];
while (count2 != count)
{
reverse[count2] = string[count];
count2++;
count--;
}
if (strcmp(reverse, string) == 0)
{
return true;
}
else
{
return false;
}
}