Skip to content

Instantly share code, notes, and snippets.

@felipecustodio
Last active December 12, 2016 06:52
Show Gist options
  • Save felipecustodio/c289636156adfad05ab975edd5257bd5 to your computer and use it in GitHub Desktop.
Save felipecustodio/c289636156adfad05ab975edd5257bd5 to your computer and use it in GitHub Desktop.
String functions for C college assignments
#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