Skip to content

Instantly share code, notes, and snippets.

@vampy
Last active August 29, 2015 13:57
Show Gist options
  • Save vampy/9451253 to your computer and use it in GitHub Desktop.
Save vampy/9451253 to your computer and use it in GitHub Desktop.
// Read a certain number of character from the keyboard
// put it in *string
// return the number of characters read
int read_string(char *string, int read_length)
{
char read_char;
int i = 0;
// weird case when we have \n on our first character
if((read_char = getchar()) != '\n')
{
// not special case
string[i] = read_char;
i++;
}
while ((read_char = getchar()) != '\n' && i < read_length)
{
string[i] = read_char;
i++;
}
// put last character
string[i] = '\0';
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment