fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct {
  6. char *characters;
  7. int length;
  8. } String;
  9.  
  10.  
  11. String *createString(){
  12. char *m;
  13. int b;
  14. int n = 0;
  15. String *theS = malloc (sizeof(String));
  16. theS->characters = malloc(1);
  17. m = theS->characters;
  18.  
  19. while(((b = getchar()) != '\n') && (b != EOF)) {
  20. *(m+n) = b;
  21. n++;
  22. m = realloc(m, n+1);
  23. }
  24. *(m+n) = '\0';
  25. theS->characters = m;
  26. theS->length = strlen(theS->characters);
  27. return theS;
  28. }
  29.  
  30. int main(void) {
  31. String *s = createString();
  32.  
  33. printf("%d: %s\n", s->length, s->characters);
  34.  
  35. // your code goes here
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 2188KB
stdin
Hello, world!
stdout
13: Hello, world!