Ir ao conteúdo

Posts recomendados

Postado

ola clã esse codigo ele reverte a frase que o usuario insere.

nesse codigo a frase que é invertida ja esta nele, preciso de ajuda para que adapte o codigo para que o o usuario insira a frase

 

#include <stdlib.h>

#include <stdio.h>

#include <string.h>



 

/**

* Reverses an array of characteres using a temporary variable

*/

void

storage_reverse_string(char *str, int i, int j)

{

while(i < j) {

 

char tmp = str[i];

str[i] = str[j];

str[j] = tmp;

 

i++;

j--;

}

}


 

/**

* Reverses an array of characteres using Bitwise operation XOR

* with no storage

*/

void

bitwise_reverse_string(char *str, int i, int j)

{

while(i < j) {

 

str[i] ^= str[j];

str[j] ^= str[i];

str[i++] ^= str[j--];

}

 

}



 

/**

* Reverses an array of characteres using sum operation

* with no storage

*/

void

sum_reverse_string(char *str, int i, int j)

{

while(i < j) {

 

str[i] += str[j];

str[j] = str[i] - str[j];

str[i++] -= str[j--];

}

}



 

/**

* Reverse sentence on string using the given function

*/

void

reverse_sentence(void (*function)(char *str, int i, int j), char *str)

{

int i = 0;

int j = strlen(str) - 1;

 

// Reverses entire string O(n)

(*function)(str, i, j);

 

int k = i;

for(; i < j; i++) {

 

if(str[i] == ' ') {

(*function)(str, k, i - 1);

k = i + 1;

}

}

(*function)(str, k, i);

}


 

int

main (int argc, char* argv[])

{

/* String that we will reverse sentence */

char str[] = "Eu sou irreversível";

 

/* Copy of the original string */

char str_copy[strlen(str)];

strncpy(str_copy, str, strlen(str));


 

printf("Before: \t%s\n", str);


 

/* Reverses sentence using a function that uses storage */

reverse_sentence(storage_reverse_string, str_copy);

printf("Storage: \t%s\n", str_copy);

memset(str_copy, 0, strlen(str_copy));

strncpy(str_copy, str, strlen(str));


 

/* Reverses sentence using a function that uses sum to avoid storage */

reverse_sentence(sum_reverse_string, str_copy);

printf("Sum: \t\t%s\n", str_copy);

memset(str_copy, 0, strlen(str_copy));

strncpy(str_copy, str, strlen(str));


 

/* Reverses sentence using a function that uses bitwise to avoid storage */

reverse_sentence(bitwise_reverse_string, str_copy);

printf("Bitwise: \t%s\n", str_copy);

memset(str_copy, 0, strlen(str_copy));

strncpy(str_copy, str, strlen(str));


 

return EXIT_SUCCESS;

}

 

  • Curtir 1
Postado

@ARcode    você quer pegar uma frase digitada no teclado , então poderia ser assim  :

int main (int argc, char* argv[])
{
    /* String that we will reverse sentence */
    char str[] = "Eu sou irreversível";
    printf("uma frase ");
    fgets(str,18,stdin);
    str[strlen(str)-1] = '\0';
    /* Copy of the original string */
    char str_copy[strlen(str)];
    strncpy(str_copy, str, strlen(str));
    printf("Before: \t%s\n", str);
    /* Reverses sentence using a function that uses storage */
    reverse_sentence(storage_reverse_string, str_copy);
    printf("Storage: \t%s\n", str_copy);

 

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

LANÇAMENTO!

eletronica2025-popup.jpg


CLIQUE AQUI E BAIXE AGORA MESMO!