Answer the question
In order to leave comments, you need to log in
How to find palindromes in a string?
I can't solve this problem in C:
Given a character string in which the words are listed separated by semicolons, and the total length is < 80. Find and print all words that are a palindrome word.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void polydrome(char **word){
int x;
int y;
int lenth = strlen(word);
for(x = 0, y = lenth-1 ; x < y; x++, y--){
if(word[x] != word[y]){
return;
}
}
printf("\n%s",word);
}
int main() {
//Инициализация переменных
char line[80] = "";
char **words[80];
int count = 0;
FILE *input;
//Проверка открытия файлов
if((input=fopen("/Users/nilinadvlr/Desktop/lab5/lab5/line.txt","r"))==NULL){
printf("Файл не открылся\n");
exit(1);
}
//Вывод всех данных
fscanf(input, "%s", line);
printf ("Исходная строка: %s\n", line);
//Разделение на отдельные слова и вывод
printf ("Результат разбивания:\n");
words[count] = strtok(line, ";");
while (words[count] != NULL){
printf("%s\n", words[count]);
count++;
words[count] = strtok(NULL, ";");
}
//Вывод слов-полидромов
printf("Слова-полидромы: \n");
for(int i = 0; i < count; i++){
polydrome(words[i]);
}
}
Answer the question
In order to leave comments, you need to log in
Have you tried reading compiler errors and warnings?
void polydrome(char **word){
-> void polydrome(char *word){
char **words[80];
->char *words[80];
The problem is that word is an array of double pointers (**). Whereas strtok returns a normal pointer.
Because of this, here
if(word[x] != word[y])
you are comparing 2 pointers, not 2 characters.
In general, I would remove the word array in principle. You can immediately call polyndrome after you have found the next word.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question