Ciao a tutti, sto scrivendo un programma in C che verifica se una stringa è palindroma oppure no.
Sorgente:
Putroppo quando eseguo, anche se inserisco una stringa palindrome (es: "otto") l'output che ottengo è " La stringa non è palindrome ". Sono certo quasi al 100% che l'errore sia nel for, ma non riesco ad individuarlo.
Spero possiate aiutarmi.
Sorgente:
C:
#include <stdio.h>
#include<string.h>
#define DIM 256
#define DEBUG
int main (int argc, char *argv[]){
char str1[ DIM ];
char str2[ DIM ];
printf("Inserire stringa -> ");
fgets(str1, DIM, stdin);
for (int i = (strlen(str1) - 2), k = 0; i >= 0; i --, k ++ ){
#ifdef DEBUG
printf("\ni = %d, k = %d\n",i,k);
#endif
str2[k] = str1[i];
#ifdef DEBUG
printf("\nstr2[k]: %c\nstr1[i]: %c\n",str2[k], str1[i]);
#endif
}
#ifdef DEBUG
printf("\nStampo lunghezza str1: %d compreso il carattere terminatore\n",strlen(str1));
#endif
if (strcmp(str1, str2) == 0){
printf("La stringa è palindrome\n");
}else{
printf("La stringa non è palindrome\n");
}
return 0;
}
Putroppo quando eseguo, anche se inserisco una stringa palindrome (es: "otto") l'output che ottengo è " La stringa non è palindrome ". Sono certo quasi al 100% che l'errore sia nel for, ma non riesco ad individuarlo.
Spero possiate aiutarmi.