Skip to content

Instantly share code, notes, and snippets.

@marquesds
Created February 8, 2013 11:34
Show Gist options
  • Save marquesds/4738351 to your computer and use it in GitHub Desktop.
Save marquesds/4738351 to your computer and use it in GitHub Desktop.
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(){
char *buffer = NULL;
char palavra;
printf("Alocando memoria\n");
buffer = (char *) malloc(1 + sizeof(char));
while (1){
printf("Digite a string para concatena-la: \n");
scanf("%s", &palavra);
if (strcmp(&palavra, "exit") == 0)
break;
/* Alocacao dinamica */
buffer = (char *)realloc(buffer, strlen(buffer) + strlen(&palavra) + sizeof(char));
printf("Memoria alocada\n");
if (!buffer) {
printf("Deu pau");
free(buffer);
return 0;
}
/* Concatenando a parada */
strcat(buffer, &palavra);
printf("String concatenada %s\n", buffer);
}
free(buffer);
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment