Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- char *getWord (FILE *fp){
- char *str = NULL;
- int c;
- size_t size = 0;
- size_t pos = 0;
- while ((c = getc(fp)) != EOF && !isspace(c) && c != '\n'){
- (pos >= size) ? str = realloc(str,
- sizeof(str) * (size) ? size *= 2 : 1) : 0;
- str[pos++] = c;
- }
- str = realloc(str, sizeof(str) * (pos + 1));
- str[pos] = '\0';
- return str;
- }
- int main(int argc, char* argv[]){
- char *str = getWord(stdin);
- printf("%s\n", str);
- free(str);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement