Advertisement
Terrah

Struct as string

May 18th, 2015
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <Windows.h>
  7.  
  8. typedef struct{
  9.     unsigned int size;
  10.     char * text;
  11. }String;
  12.  
  13. //Let the user input a string
  14. String * ReadString(int max_size);
  15.  
  16. //Test print a string
  17. void PrintString(String * text);
  18.  
  19. //Use this for strings instead of free
  20. void FreeString(String * string);
  21.  
  22. //Create a string
  23. String * CreateString(const char * text);
  24.  
  25.  
  26. String * CreateString(const char * text){
  27.  
  28.     String * ret = (String*)malloc(sizeof(String));
  29.  
  30.     if (ret == NULL)
  31.         return NULL;
  32.  
  33.     ret->size = strlen(text)+1;
  34.     ret->text = (char*)malloc(ret->size);
  35.  
  36.     if (ret->text != NULL)
  37.         strcpy(ret->text, text);
  38.     else{
  39.         free(ret);
  40.         return NULL;
  41.     }
  42.  
  43.     return ret;
  44. }
  45.  
  46. void FreeString(String * string){
  47.  
  48.     if (string != NULL){
  49.  
  50.         //Free the pointer inside the struct first if its not null
  51.         if (string->text != NULL)
  52.             free(string->text);
  53.  
  54.         //free the rest of the struct
  55.         free(string);
  56.     }
  57. }
  58.  
  59. void PrintString(String * text){
  60.     if (text == NULL)
  61.         printf("String is empty!");
  62.     else
  63.         printf("Size (in bytes): %u\nData: %s\n",text->size,text->text);
  64. }
  65.  
  66. String * ReadString(int max_size){
  67.  
  68.     char * buffer = (char*)malloc(max_size);
  69.     String * ret;
  70.  
  71.     if (buffer == NULL)
  72.         return NULL;
  73.  
  74.     fgets(buffer, max_size, stdin);
  75.  
  76.     //strip endline
  77.     buffer[strlen(buffer) - 1] = NULL;
  78.  
  79.     ret = CreateString(buffer);
  80.     free(buffer);
  81.  
  82.     return ret;
  83. }
  84.  
  85. void main(){
  86.  
  87.     //Using the string raw:
  88.     String Terrah;
  89.     String * Kluffu;
  90.     String * First;
  91.     String * Last;
  92.     char * split;
  93.     char temp;
  94.  
  95.     Terrah.text = "Hello this is Terrah";
  96.     Terrah.size = strlen(Terrah.text) + 1;
  97.  
  98.     PrintString(&Terrah);
  99.     //Here you'd normally free the data, but since this is compiled in the compilier
  100.     //we'd crash if we tried to deallocate this memory
  101.     //FreeString(&Terrah);
  102.     //We never do this for non pointers
  103.  
  104.     //dynamically
  105.     printf("First and last name?: ");
  106.     Kluffu = ReadString(100);
  107.     PrintString(Kluffu);
  108.  
  109.     //Split the name
  110.    
  111.     //find the space
  112.     //strstr returns null if it didnt find the substring or a pointer to the begining of it
  113.     split = strstr(Kluffu->text," ");
  114.  
  115.     //Split if we found it
  116.     if (split != NULL){
  117.  
  118.         //temporarily null terminate
  119.         temp = split[0];
  120.         split[0] = NULL;
  121.  
  122.         First = CreateString(Kluffu->text);
  123.         Last = CreateString(split+1);
  124.  
  125.         //Restore
  126.         split[0] = temp;
  127.  
  128.         PrintString(First);
  129.         PrintString(Last);
  130.  
  131.         //Delete
  132.         FreeString(First);
  133.         FreeString(Last);
  134.  
  135.         First = NULL;
  136.         Last = NULL;
  137.     }
  138.  
  139.     //If you wanted to get info out of the string here, you'd use sscanf
  140.     //like you would scanf but with the text buffer as the first argument
  141.     //sscanf(Kluffu->text, "", "");
  142.  
  143.     FreeString(Kluffu);
  144.  
  145.     _getch();
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement