Created
April 10, 2017 21:30
-
-
Save matoushybl/f32ebffce612888ea7ee4ea60f35d2aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
unsigned PocetMezer(char *radek) { | |
if (radek == NULL) { | |
return 0; | |
} | |
unsigned counter = 0; | |
for (int i = 0; radek[i] != '\0'; i++) { | |
if(*(radek + i) == ' ') { | |
counter++; | |
} | |
} | |
return counter; | |
} | |
int NactiRadek(char **radek, FILE *file) { | |
if (radek == NULL || file == NULL) { | |
return -1; | |
} | |
if (*radek != NULL) { | |
return -2; | |
} | |
int len = 10; | |
char* buffer = (char*) malloc(len* sizeof(char)); | |
if (buffer == NULL) { | |
return -3; | |
} | |
int currentChar = fgetc(file); | |
int counter = 0; | |
if(feof(file)) { | |
free(buffer); | |
return -4; | |
} | |
while(currentChar != '\0' && currentChar != '\n' && !feof(file)) { | |
if (counter >= len - 1) { | |
int newLen = 2*len; | |
char *newBuffer = (char*)malloc(newLen* sizeof(char)); | |
for (int i = 0; i < len; i++) { | |
*(newBuffer + i) = *(buffer + i); | |
} | |
free(buffer); | |
buffer = newBuffer; | |
len = newLen; | |
} | |
*(buffer + counter) = (char) currentChar; | |
counter++; | |
currentChar = fgetc(file); | |
} | |
*(buffer+counter)='\0'; | |
// nasobeni mezer | |
char* out = (char*) malloc((counter + PocetMezer(buffer)* sizeof(char))); | |
int outAppend = 0; | |
for(int i = 0; i < (counter+PocetMezer(buffer)); i++) { | |
*(out+outAppend+i)= *(buffer + i); | |
if(*(buffer + i) == ' ') { | |
outAppend++; | |
*(out+outAppend+i)=' '; | |
} | |
} | |
*radek = out; | |
free(buffer); | |
return counter; | |
} | |
int PrevedPole(char* radek, unsigned **vystup) { | |
int len = 0; | |
for (int i = 0; *(radek + i) != '\0'; i++) { | |
len++; | |
} | |
unsigned *out = (unsigned*) malloc(len* sizeof(unsigned)); | |
for(int i = 0; i < len; i++) { | |
*(out+i) = (unsigned) *(radek + i); | |
printf("%u ", (unsigned) *(radek + i)); | |
} | |
*vystup = out; | |
return 0; | |
} | |
int main(void) { | |
char* out = NULL; | |
FILE* in = fopen("../input.txt", "r"); | |
int ret = NactiRadek(&out, in); | |
if(ret < 0) { | |
fclose(in); | |
return -1; | |
} | |
printf("%s\n", out); | |
unsigned* ints = NULL; | |
PrevedPole(out, &ints); | |
fclose(in); | |
free(out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment