#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getSentence(char userSentence[]);
int breakSentence_andCompare(char userSentence[] , char compareSentence[]);
#define MAX_SENTENCE 100
int main()
{
int len = 0;
char userSentence[MAX_SENTENCE] = {'o','k',0};
char compareSentence[MAX_SENTENCE] = {'o',0};
getSentence(userSentence);
len = breakSentence_andCompare(userSentence,compareSentence);
}
/*
This function is asking the user to input info.
input:user input string array - char userSentence[].
output:none.
*/
void getSentence(char userSentence[])
{
printf("Hello And Welcome To The Palindrome Cheker Made By xXTH3 SKIRT CH4S3RXx");
printf("\nPlease Enter A Sentence: ");
fgets(userSentence,MAX_SENTENCE,stdin);
}
/*
This function takes the input of the user and input it into another string backwards.
input:user input string array - char userSentence[], backward user input string array - char compareSentence[].
output:strcmp value.
*/
int breakSentence_andCompare(char userSentence[] , char compareSentence[])
{
int i = 0;
int z = 0;
int len = 0;
int cmp = 0;
len = strlen(userSentence);
len -= 1;
for (i = len ; i >= 0 ; i--)
{
compareSentence[z] = userSentence[i];
printf("%s",compareSentence);
z++;
}
printf("\nuser: %s! compare: %s!",userSentence,compareSentence);
cmp = strcmp(userSentence,compareSentence);
printf("\n%d",&cmp);
return cmp;
}
This program checks if inputted string is palindrome, To simply explain how it works:
- It takes user input - String.
- It Takes the user string and input is backwards in another string.
- It compares the strings.
I have a really strange problem in the function and that's the strcmp
return value. For some reason, when both strings have the same characters like ABBA the strcmp will return that the value of one of them is bigger. I'd really love to know what is the problem and how can I fix it.
P.S
When I were searching the problem I thought that it might be connected to fact that the user input string might contain \n
from the enter key; is that possible?
And please understand that this isn't a whole code. The code is missing the output part.