FORUM DARKERS

Tecnologia & Informática => Programação => C/C#/C++ => Topic started by: Anonymous on 23 de August , 2006, 07:31:10 PM

Title: Desafio de strings
Post by: Anonymous on 23 de August , 2006, 07:31:10 PM
Heys! Ja que estamos numa onda de desafios, venho aqui propor mais um gostaria que quando postassem os vossos codes  tivessem comentados se possivel...
Bem agora vamos ao desafio:

Escreva um programa em C que tenha como output primeiramente uma string introduzida pela ordem em que foi escrita e depois outra pela ordem contraria...

tipo introduz-se a string "Darkers" e devolve novamente "Darkers" e o oposto "srekraD"

Abraços,
Title: Re: Desafio de strings
Post by: Anonymous on 23 de August , 2006, 07:33:59 PM
Ah e desculpem esqueçi de postar o meu code... sorry por fazer outro post seguido como nao tem a funcao de edit fica dificil...

#include <stdio.h>

main()

{
        char string[100];
        char *ptr=string;

        printf("Introduza uma string:\n");
        gets(string);
        if (*ptr=='\0')
                return 0;

        while (*ptr!='\0')
        {putchar(*ptr);
                ptr++;
        }
                while (ptr>=string)
        {
                putchar(*ptr);
                ptr--;
        }}
Title: Re: Desafio de strings
Post by: Anonymous on 23 de August , 2006, 09:28:15 PM
Cara muito simples fazer isso que voce pediu....

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

char* Invert(char arg[])
{
    int len = strlen(arg);//Pega o numero de caracteres
    int v = 0;//variavel q contara o num de caractes para retorno
    char *ret = new char;//variavel de retorno
    while( len >= 0 )//enquanto len maior ou igual a zero
    {
ret[v] = arg[len-1];//copia
len--;//Descrementa
v++;//Encrementa
    }
    return ret;//returna
}
int main()
{
    char *msg = new char;
printf("Digite uma palavra: ");
gets(msg);
printf("Original: %s\nModificado: %s\n", msg, Invert(msg));
getch();
    return 0;
}
Title: Re: Desafio de strings
Post by: Anonymous on 23 de August , 2006, 11:30:37 PM
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char *str = (char*)malloc(1000);
   
    fgets(str, 1000, stdin); //pega string
    puts(str); //mostra string
    puts(strrev(str)); //mostra string invertida
    free(str);
   
    system("pause > nul");
}

Abraço.
Title: Re: Desafio de strings
Post by: Anonymous on 24 de August , 2006, 12:12:32 AM
Hehe crypthief, eu nem gostei de ter usado strrev(), até porque não seria bem um desafio, então fiz outro usando uma função que eu mesmo criei.

#include <stdio.h>
#include <stdlib.h>

int reverse(char *str) {
    if(*str!=0) return reverse(str+1) + putchar(*str);
}

int main() {
    char str[1000];
   
    fgets(str, 1000, stdin);
    puts(str);
    reverse(str);
   
    system("pause > nul");
}
Title: Re: Desafio de strings
Post by: Anonymous on 25 de August , 2006, 12:19:16 AM
Nossa esse desafio todo mundo respondeu =L