N
N
nilinad_vlr2021-11-18 13:04:06
C++ / C#
nilinad_vlr, 2021-11-18 13:04:06

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");
}


With the English alphabet, everything works correctly, but with the Russian alphabet, it already turns out to be complete nonsense. What is the problem. MB need to change the file encoding or localization how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-11-18
@wataru

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 question

Ask a Question

731 491 924 answers to any question