{"id":1164,"date":"2021-08-30T20:01:39","date_gmt":"2021-08-30T23:01:39","guid":{"rendered":"http:\/\/www.galirows.com.br\/meublog\/programacao\/?p=1164"},"modified":"2021-08-30T20:10:17","modified_gmt":"2021-08-30T23:10:17","slug":"mostrar-texto-string-ao-contrario","status":"publish","type":"post","link":"http:\/\/www.galirows.com.br\/meublog\/programacao\/mostrar-texto-string-ao-contrario\/","title":{"rendered":"Mostrar texto\/string ao contr\u00e1rio"},"content":{"rendered":"\n<p>Trago um exerc\u00edcio resolvido simples (tanto o enunciado, quanto a resolu\u00e7\u00e3o), mas que traz dificuldade por se tratar da manipula\u00e7\u00e3o de textos, e isso normalmente traz algumas quest\u00f5es dificultadoras.<\/p>\n\n\n\n<p><strong>Enunciado<\/strong>: fa\u00e7a um algoritmo que leia um texto e mostre o texto ao contr\u00e1rio.<\/p>\n\n\n\n<p>A solu\u00e7\u00e3o \u00e9 mostrada nas linguagens Python, C e C++. A solu\u00e7\u00e3o \u00e9 bem parecida nas tr\u00eas linguagens. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solu\u00e7\u00e3o em C<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\nint main(void) {\n    char palavra[100];\n    fgets(palavra,100,stdin);\n    printf(\"%s-%i\", palavra, strlen(palavra)-1);\n\n    for (i=strlen(palavra)-2; i &gt;=0; i--) {\n        printf(\"%c\", palavra[i]);\n    }\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Mostrar texto\/string ao contr\u00e1rio\" width=\"685\" height=\"385\" src=\"https:\/\/www.youtube.com\/embed\/oS_joBAhBaI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Solu\u00e7\u00e3o em C++<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Leitura de string e mostrar ao contr\u00e1rio [C++]\" width=\"685\" height=\"514\" src=\"https:\/\/www.youtube.com\/embed\/BCFECsKyKVQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Solu\u00e7\u00e3o em Python<\/h2>\n\n\n\n<p>Para a solu\u00e7\u00e3o em Python \u00e9 importante saber que a fun\u00e7\u00e3o range() tamb\u00e9m pode ser utilizada de forma decrescente. Veja que a fun\u00e7\u00e3o \u00e9 definida como <code>range(start,end,step)<\/code>. Se utilizar um <em>step<\/em> negativo ser\u00e1 gerado os n\u00fameros em ordem decrescente.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">palavra = input()\n\nfor i in range(len(palavra)-1,-1,-1):\n  #print(i,palavra[i])\n  print(palavra[i], end = '')<\/code><\/pre>\n\n\n\n<p>Veja que a solu\u00e7\u00e3o tem muitos -1 no range(). Isso porque: \u00e9 preciso diminuir uma unidade do tamanho da palavra (porque o \u00edndice da string come\u00e7a em zero); o passo -1 \u00e9 para ter gerar valores decrescentes; o final em -1 \u00e9 necess\u00e1rio porque sen\u00e3o o <em>for <\/em>iria parar com o valor 1 para a vari\u00e1vel <em>i<\/em>, o que \u00e9 insuficiente para chegar na primeira posi\u00e7\u00e3o do vetor (posi\u00e7\u00e3o zero).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trago um exerc\u00edcio resolvido simples (tanto o enunciado, quanto a resolu\u00e7\u00e3o), mas que traz dificuldade por se tratar da manipula\u00e7\u00e3o de textos, e isso normalmente traz algumas quest\u00f5es dificultadoras. Enunciado: fa\u00e7a um algoritmo que leia um texto e mostre o texto ao contr\u00e1rio. A solu\u00e7\u00e3o \u00e9 mostrada nas linguagens Python, C e C++. A solu\u00e7\u00e3o [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,5,4],"tags":[],"class_list":["post-1164","post","type-post","status-publish","format-standard","hentry","category-c","category-python","category-videos"],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/1164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/comments?post=1164"}],"version-history":[{"count":4,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/1164\/revisions"}],"predecessor-version":[{"id":1451,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/1164\/revisions\/1451"}],"wp:attachment":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/media?parent=1164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/categories?post=1164"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/tags?post=1164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}