'Finding palindromes in C
I am new to C language and would like to understand the code. The standard input stream is given a string consisting of words with a maximum length of 100 characters. Words consist of Latin characters and are separated by one space. Write to standard output a string containing only palindromes. A palindrome is a word that reads the same in both directions. For example, for input: dad peep aaas. To the exit: dad peep
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void) {
int i = 0;
char word[100];
int ch, bk, fr;
while (EOF != (ch = getchar())) {
if (isspace(ch) != 0) {
if (i > 0) {
fr = 0;
bk = i - 1;
while (word[fr] == word[bk]) {
++fr;
--bk;
}
if (fr < bk)//not palindromes
i = 0;
else {
while (i > 0)
printf("%c", word[--i]);
printf("%c", ch);
}
}
if (ch == '\n')
break;
}
else {
word[i++] = tolower(ch);//check i < sizeof(word)
}
}
}
It is not clear to me how the algorithm selects the words we need. I would like to know this step by step due to my extreme inexperience.
Solution 1:[1]
The code is bad and has undefined behavior.
For example let's assume that the input consists from one character 'A' and a space "A ". The word "A" is a palindrome.
But in this while loop
while (word[fr] == word[bk]) {
++fr;
--bk;
}
where initially fr is equal to 0 and bk is equal to 0 due to these assignments (I think fr means front and bk means back)
if (i > 0) {
fr = 0;
bk = i - 1;
//...
there will be access to memory beyond the array word. That is in the first iteration of the while loop the condition evaluates to the logical true.
word[fr] == word[bk]
that is equivalent to
word[0] == word[0]
So the variable bk will be decremented. And in the next iteration of the loop the condition will look like
word[1] == word[-1]
^^^^^^^^
My advice: do not search a bad code in the internet and do not spend your time to understand it.
As for you question then within this while loop
while (EOF != (ch = getchar())) {
characters are being written in the character array word starting from the position i equal to 0 until a white space character is encountered.
if (isspace(ch) != 0) {
//...
}
else {
word[i++] = tolower(ch);//check i < sizeof(word)
}
If a white space is encountered
if (isspace(ch) != 0) {
then the variable i points to the position in the array word after the last written character and the program checks whether the sequence of characters stored in word of the length equal to i is a palindrome.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
