Skip to content

Instantly share code, notes, and snippets.

@carlosbrando
Created May 6, 2011 17:27
Show Gist options
  • Save carlosbrando/959381 to your computer and use it in GitHub Desktop.
Save carlosbrando/959381 to your computer and use it in GitHub Desktop.
Uma versão muito simples da função gets() da biblioteca padrão.
#include <stdio.h>
/* Uma versão muito simples da função gets()
* da biblioteca padrão. */
char *xgets(char *s)
{
char ch, *p;
int t;
p = s; /* gets() devolve um ponteiro para s */
for (t = 0; t < 80; ++t) {
ch = getchar();
switch(ch) {
case '\n':
s[t] = '\0'; /* termina a string */
return p;
case '\b':
if (t > 0) t--;
break;
default:
s[t] = ch;
}
}
s[80] = '\0';
return p;
}
main(void)
{
char message[80];
xgets(message);
printf("%s\n", message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment