Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Autor : Tiago Portela
- Email : sapitando@gmail.com
- Sobre : Compilado com TDM-GCC 5.10 64-bit e LCC-Win32 64-bit;
- Obs : Apenas tentando aprender algoritimos, sozinho, por hobby. */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <assert.h>
- #define BUFFERSIZE 8
- typedef struct _Node {
- char *lpBuffer;
- struct _Node *LpProxNode;
- } Node_t, *LpNode_t;
- int main(void)
- {
- char acBuffer[BUFFERSIZE] = {0};
- LpNode_t lpNode = (LpNode_t)malloc(sizeof(Node_t));
- assert(lpNode!=NULL);
- LpNode_t lpHeadNode = lpNode;
- char* lpBuffer = (char*)calloc(1,sizeof(char));
- assert(lpBuffer!=NULL);
- char cChar;
- printf("Enter a message: \n");
- // Exit when Enter is pressed
- /* while(true)
- {
- assert(fgets(acBuffer, BUFFERSIZE, stdin)!=NULL);
- lpNode->lpBuffer = malloc((strlen(acBuffer) + 1) * sizeof(char));
- assert(lpNode->lpBuffer!=NULL);
- strcpy(lpNode->lpBuffer, acBuffer);
- if(lpNode->lpBuffer[strlen(acBuffer) - 1] == '\n')
- {
- lpNode->lpBuffer[strlen(acBuffer) - 1] = '\0';
- lpNode->LpProxNode = NULL;
- break;
- }
- lpNode->LpProxNode = (LpNode_t)malloc(sizeof(Node_t));
- lpNode = lpNode->LpProxNode;
- assert(lpNode!=NULL);
- }*/
- // Exits the loop if the line is empty(Improving code).
- while(true)
- {
- assert(fgets(acBuffer, BUFFERSIZE, stdin)!=NULL);
- lpNode->lpBuffer = malloc((strlen(acBuffer) + 1) * sizeof(char));
- assert(lpNode->lpBuffer!=NULL);
- strcpy(lpNode->lpBuffer, acBuffer);
- if(acBuffer[strlen(acBuffer) - 1] == '\n')
- lpNode->lpBuffer[strlen(acBuffer) - 1] = '\0';
- if(strlen(acBuffer) == (BUFFERSIZE - 1) && (acBuffer[strlen(acBuffer) - 1] != '\n'))
- {
- cChar = fgetc(stdin);
- if(cChar != '\n')
- ungetc(cChar, stdin);
- }
- if(acBuffer[0] == '\n')
- {
- lpNode->LpProxNode = NULL;
- break;
- }
- lpNode->LpProxNode = (LpNode_t)malloc(sizeof(Node_t));
- lpNode = lpNode->LpProxNode;
- assert(lpNode!=NULL);
- }
- printf("\nPseudo String :\n");
- lpNode = lpHeadNode;
- while(lpNode != NULL)
- {
- printf("%s", lpNode->lpBuffer);
- lpNode = lpNode->LpProxNode;
- }
- printf("\n\nMemory blocks:\n");
- lpNode = lpHeadNode;
- while(lpNode != NULL)
- {
- printf("Block \"%7s\" size = %lu\n", lpNode->lpBuffer, (long unsigned)(strlen(lpNode->lpBuffer) + 1));
- lpNode = lpNode->LpProxNode;
- }
- printf("\nConcatenated string:\n");
- lpNode = lpHeadNode;
- while(lpNode != NULL)
- {
- lpBuffer = (char*)realloc(lpBuffer, (strlen(lpBuffer) + strlen(lpNode->lpBuffer)) + 1);
- strcat(lpBuffer, lpNode->lpBuffer);
- lpNode = lpNode->LpProxNode;
- }
- printf("%s", lpBuffer);
- printf("\n\n");
- // Deallocate memory
- lpNode = lpHeadNode;
- while(lpNode != NULL)
- {
- lpHeadNode = lpNode->LpProxNode;
- free(lpNode->lpBuffer);
- free(lpNode);
- lpNode = lpHeadNode;
- }
- lpBuffer = (char*)realloc(lpBuffer, 0);
- lpBuffer = NULL;
- if((lpNode == NULL) && (lpBuffer == NULL))
- {
- printf("Deallocate memory = %s", (char*)lpNode);
- }
- printf("\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement