Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include "FileHandler.h"
  2. #include< stddef.h >
  3. void copy(char*, char*);
  4. void append(char*, char*);
  5. void split(char*, char,char**);
  6. int main()
  7. {
  8.     int num[2]={0,0}, i = 0, j = 0,mark;
  9.     char* fileinput = readAllTextFromFile("./input.txt");
  10.     while (*(fileinput + i) != NULL) {
  11.         while (*(fileinput + i) != '\n' && *(fileinput + i) != NULL) {
  12.             if (j < 2) num[j]++; else if (j == 2) mark = i;
  13.             i++;
  14.         }
  15.         i++;
  16.         j++;
  17.     }
  18.     char* str = malloc(num[0] * sizeof(char));
  19.     char* copyed = malloc(num[0] * sizeof(char));
  20.     char* inputappend = malloc(num[1] * sizeof(char));
  21.     char splitchar = *(fileinput + mark);
  22.     char** splitresult = malloc((num[0]+num[1]) * sizeof(char));
  23.     for (i = 0; i < num[0]; i++) {
  24.         *(str + i) = *(fileinput + i);
  25.     }
  26.     *(str + num[0]) = '\0';
  27.     printf("Input string:%s\n\n",str);
  28.     for (i = 0; i < num[1]; i++) {
  29.         *(inputappend + i) = *(fileinput + i+1+ num[0]);
  30.     }
  31.     *(inputappend + num[1]) = '\0';
  32.     printf("append text:%s\n", inputappend);
  33.     copy(str, copyed);
  34.     append(str, inputappend);
  35.     printf("append result:%s\n\n", str);
  36.     printf("original string:%s\n\n", copyed);
  37.     printf("split char:%c\n", splitchar);
  38.     split(str, splitchar,splitresult);
  39.     printf("split resilt:%s\n", splitresult);
  40.     return 0;
  41. }
  42. void copy(char* src, char* dist) {
  43.     int i = 0;
  44.     while (*(src + i) != '\0') {
  45.         *(dist + i) = *(src + i);
  46.         i++;
  47.     }
  48.     *(dist + i) = '\0';
  49. }
  50.  
  51. //append the text after target.
  52. void append(char* target, char* text) {
  53.     int i = 0,j=0;
  54.     while (*(target + i) != '\0') {
  55.         i++;
  56.     }
  57.     while (*(text+ j) != '\0') {
  58.         j++;
  59.     }
  60.     target=realloc(target, (i + j) * sizeof(char));
  61.     for (int k = 0; k < j; k++) {
  62.         *(target + i + k) = *(text + k);
  63.     }
  64.     *(target + i + j) = '\0';
  65. }
  66.  
  67. //split src string by target,  then output by segments.
  68. void split(char* src, char target,char** segments) {
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement