Last active
December 12, 2016 06:52
-
-
Save felipecustodio/c289636156adfad05ab975edd5257bd5 to your computer and use it in GitHub Desktop.
String functions for C college assignments
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> | |
/* read string from stdin */ | |
char* readString(char end) { | |
char* string = NULL; | |
char value = '@'; | |
int counter = 0; | |
while (value != end) { | |
scanf("%c", &value); | |
string = (char*)realloc(string, sizeof(char) * counter + 1); | |
string[counter] = value; | |
counter++; | |
} | |
string[counter-1] = '\0'; | |
return string; | |
} | |
/* read string from file */ | |
char* readfString(FILE* fp, char end) { | |
char* string = NULL; | |
char value = '@'; | |
int counter = 0; | |
while (value != end) { | |
fscanf(fp, "%c", &value); | |
string = (char*)realloc(string, sizeof(char) * counter + 1); | |
string[counter] = value; | |
counter++; | |
} | |
string[counter-1] = '\0'; | |
return string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment