Localhost Posted October 18, 2009 at 11:19 AM Report Share #292157 Posted October 18, 2009 at 11:19 AM 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 More sharing options...
zecapistolas Posted October 18, 2009 at 11:27 AM Report Share #292159 Posted October 18, 2009 at 11:27 AM 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 More sharing options...
Tharis Posted October 18, 2009 at 11:35 AM Report Share #292163 Posted October 18, 2009 at 11:35 AM #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 More sharing options...
Localhost Posted October 18, 2009 at 11:37 AM Author Report Share #292164 Posted October 18, 2009 at 11:37 AM Shii, não percebi nada do code, ainda não cheguei a essa parte de buffers etc. Mas estou a caminho, estou agora em ponteiros... here since 2009 Link to comment Share on other sites More sharing options...
Localhost Posted October 18, 2009 at 11:43 AM Author Report Share #292165 Posted October 18, 2009 at 11:43 AM Tharis a minha ideia era deixar o code comprimido, além de que (ainda) nao ouvi falar de alguns conceitos de que meteste para aí para o meio, mas agradeço o post 😛 here since 2009 Link to comment Share on other sites More sharing options...
Baderous Posted October 18, 2009 at 02:25 PM Report Share #292188 Posted October 18, 2009 at 02:25 PM O que o Tharis postou foi exactamente aquilo que eu já tinha postado no teu outro tópico. Link to comment Share on other sites More sharing options...
Localhost Posted October 18, 2009 at 04:51 PM Author Report Share #292214 Posted October 18, 2009 at 04:51 PM Pois, daí eu ter dito que ainda não tinha conhecimento de alguns conceitos, como disse no outro tópico... here since 2009 Link to comment Share on other sites More sharing options...
Rondineli Couto Posted December 8, 2018 at 11:01 PM Report Share #612746 Posted December 8, 2018 at 11:01 PM 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 More sharing options...
Rondineli Couto Posted December 8, 2018 at 11:02 PM Report Share #612747 Posted December 8, 2018 at 11:02 PM 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now