Палиндром на C. Почему-то, когда я ввожу массив с первыми пробелами, он мне выводит эти пробелы, хотя там даже нет output?

Рейтинг: 0Ответов: 0Опубликовано: 24.03.2023
#include <stdio.h>
//palindrome finder

int main() {
    char str[100];
    int counter = 0;
    for(int i = 0; i < 100; i++){
        scanf("%c", &str[i]);
        if(str[i] == '\n'){ //to stop writing the massive if user types endl
            break; //alike scanf "%s"
        } else if(str[i] >= 97 && str[i] <= 122){ //to change size of letters for a comparison
            str[i] = str[i] - 32;
        }
        counter++; //counting a length of the massive
    }
    int shift = 0; //the position of char in the massive
    int index = 0; //identifier for the result
    //verifying of palindrome
    for(int i = 0; i < counter; i++){
        if(counter % 2 != 0 && str[shift] == str[counter - 1 - shift]){ //wether it's even or not
            index++;    //also we compare massive from the scratch to the end and backwards
            shift++;    //to find out if it's palindrome
        } else if (counter % 2 == 0 && str[shift] == str[counter - 1 - shift]){
            index++;
            shift++;
        } else { //if we come across with mismatch, we break the cycle
            break;
        }
    }
    int space_counter = 0; //if the massive was filled with spaces
    for(int i = 0; i < counter; i++){
        if(str[i] == 32){
            space_counter++;
        }
    }
    if (str[0] == '\n'){ //if the massive has only endl
        index = -1;
    }
    if (index == counter && space_counter != counter){ //comparison
        printf("YES"); //logically, if previous cycle went through successfully, we could get coinsidence of index and counter
    } else {
        printf("NO");
    }
    return 0;
}

Ответы

Ответов пока нет.