#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* revert(char* s){
int tam = strlen(s); // tamanho da string
char* revertido = malloc(tam * sizeof(char) + 1); // alocação dinâmica do vetor de String revertido
if(revertido == NULL){ // verifica se o vetor alocado tem memória
printf("Não há memória suficiente!\n");
exit(1);
}
for(int i = 0; i < tam; i++) // percore até o tamanho da string passada
revertido[i] = s[tam - i - 1]; // atribui ao vetor revertido o índice em forma decrescente do vetor s
revertido[tam] = '\0'; // garante que o último indice do vetor revertido é o caracter nulo
return revertido; // retorna o vetor com os caracteres ja revertidos
}
int main(void){
char texto[] = "Cachorro";
char* revertido = revert(texto);
printf("%s\n", revertido);
free(revertido);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: