All pastes #573734 Raw Edit

Palindromo

public c v1 · immutable
·published 2007-06-18 07:58 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>main() {  char vetor[50], vetorinv[50], aux;  int i, j, w, k;  printf("Digite uma string: ");  fgets(vetor, 50, stdin);    //Primeiro passo: Eliminar espaços em branco no meio da string;  for(i=49; i>=0; i--) {    if ((vetor[i]==' ')||(vetor[i]=='\n')) {      for(j=i; j<49; j++) {        vetor[j] = vetor[j+1];      }    }  }    //Segundo passo: Encontrar o final da String;  for(i=0; i<50; i++) {    if (vetor[i]=='\0') {      w=i-1; //O final da string é antes do \0 ;)      break;    }  }    //Terceiro passo: Inverter a string em um novo vetor;  for(i=w; i>=0; i--) {    vetorinv[w-i] = vetor[i];  }    //Quarto passo: Comparar as strings normal e invertida;  j = 0;  for(i=0; i<w; i++) {    if (vetor[i]!=vetorinv[i]) {      j++;    }  }    //Quinto passo: Retornar o resultado;  if (j>0) {    printf("A palavra digitada não é palíndromo. %i letras diferentes.\r\n", j);  } else {    printf("A palavra digitada é palíndromo.\r\n");  }    for (i=0; i<=w; i++) {    printf("%c", vetor[i]);  }  printf("\r\n");  for (i=0; i<=w; i++) {    printf("%c", vetorinv[i]);  }  printf("\r\n");    return 0;}