Jump to content

Virar Strings ao contrário [Meu code]


Localhost
 Share

Recommended Posts

Olá pessoal, eu no outro dia abri aqui um tópico com a dúvida de como virar strings ao contrário, tive logo respostas, no entanto tive a estudar e a pensar em como transformar o código mais pequeno e utilizar o minimo de variaveis possivel e a verdade é que consegui chegar a um código bem engraçado  😛

#include <stdio.h> 
#include <conio.h>

int main()
{
char string[1024]; 
int i, tamanho; 
printf("Digite uma string: "); 
gets(string); 
tamanho = strlen(string); 
printf("A string invertida -> "); 
for(i=tamanho-1; i>=0; i--) 
{
	printf("%c" ,string[i]);
}
getch(); 
return 0;
}

É este o código , quais as diferenças? Começa logo por usar uma string a menos e depois ao fazer o printf ele nao passa a string inteira como argumento mas sim caracter a caracter, vejam o code e digam-me se gostam, aproveito para agradecer ao miguel1234, ao baderous e ao pedrosorio que me ajudaram  :smoke:

here since 2009

Link to comment
Share on other sites

Parece-me bastante simples e funcional.... Só tenho uma coisa a apontar, não utilizes "gets" para ler string's, porque "O gets() é uma função manhosa, devido ao possivel buffer overflow" (Via Wiki Portugal-a-Programar)....

Para ler string's eu utilizo o seguinte trecho de código:

/* Ler os dados até se introduzir ENTER */
scanf("%[^\n]", cmd);
/* Limpar o buffer */
scanf("%*[^\n]"); scanf("%*c");

(Via Wiki Portugal-a-Programar)

cumps  😛

Link to comment
Share on other sites

#include <stdio.h>
#include <conio.h>

int main()
{
        char c, string[1024];
        int i, tamanho;
        printf("Digite uma string: ");
        fgets(stdin, 1024, string);
        tamanho = strlen(string);
        for(i=0; i<tamanho/2; i++)
        {
                c = string[i];
                string[i] = string[tamanho-i-1];
                string[tamanho-i-1] = c;
        }
        printf("A string invertida -> %s\n", string);
        getch();
        return 0;
}

Assim ela fica invertida na memória e em metade do tempo... 😛

Já agora, quando quiseres imprimir um caracter, usa o putchar, que é muito mais rápido que o printf. 😛

Link to comment
Share on other sites

Em 18/10/2009 às 09:27, zecapistolas disse:

Parece-me bastante simples e funcional.... Só tenho uma coisa a apontar, não utilizes "gets" para ler string's, porque "O gets() é uma função manhosa, devido ao possivel buffer overflow" (Via Wiki Portugal-a-Programar)....

Para ler string's eu utilizo o seguinte trecho de código:

 


/* Ler os dados até se introduzir ENTER */
scanf("%[^\n]", cmd);
/* Limpar o buffer */
scanf("%*[^\n]"); scanf("%*c");
 

 

(Via Wiki Portugal-a-Programar)

cumps  😛

mas para quem ta começando até que é bem util.

Link to comment
Share on other sites

Em 18/10/2009 às 09:35, Tharis disse:

 


#include <stdio.h>
#include <conio.h>

int main()
{
        char c, string[1024];
        int i, tamanho;
        printf("Digite uma string: ");
        fgets(stdin, 1024, string);
        tamanho = strlen(string);
        for(i=0; i<tamanho/2; i++)
        {
                c = string[i];
                string[i] = string[tamanho-i-1];
                string[tamanho-i-1] = c;
        }
        printf("A string invertida -> %s\n", string);
        getch();
        return 0;
}
 

 

Assim ela fica invertida na memória e em metade do tempo... 😛

Já agora, quando quiseres imprimir um caracter, usa o putchar, que é muito mais rápido que o printf. 😛

Po teu código me ajudou legal, um detalhe bobo, indenta kkkkk....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.