Answer the question
In order to leave comments, you need to log in
Help with a problem in C?
Unable to solve the problem.
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 isPalindrom (char word[]){
int length = strlen(word);
int i,j;
for (i = 0, j = length-1; i < j; i++, j--){
if (word[i] != word[j]){
printf(" - Не палидром");
return;
}
}
printf(" - Палидром");
}
int main(void){
//Инициализация переменных
char str[80];
char* word;
FILE *input;
//Проверка открытия файла
if((input = fopen("/Users/nilinadvlr/Desktop/lab5/lab5/stre.txt","r"))==NULL){
printf("Файл не открылся\n");
exit(1);
}
//Сканирование и вовыд строки
fscanf(input, "%s", str);
printf("Строка: %s", str);
//Разделение на отдельные слова
word=strtok(str,";");
//Анализ каждого слова
while (word != NULL) {
printf("\nСлово: %s",word);
isPalindrom(word);
word = strtok (NULL, ";");
}
printf("\n");
}
Answer the question
In order to leave comments, you need to log in
Since it doesn't work with Russian letters, it means that Russian letters are in some utf-8 format. Then one character can be given by several bytes. Your char operation checks that the words are polydromes byte-by-byte, which in the case of utf-8 is not the same as character-by-character.
Try or change the file encoding to something like CP1251.
Or rewrite your program using wchar_t*
wcstok ()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question