How to implement the function gets() to input a string in C

1 Answer

0 votes
#include <stdio.h>
  
#define LEN 30

void my_gets(char s[]);

int main(int argc, char **argv) 
{ 
    char s[LEN];
   
    my_gets(s);

    printf("s = %s\n", s); 
    
    return 0;
}

void my_gets(char s[]) 
{ 
    int ch, i = 0; 
    
    while ( (ch = getchar()) != EOF && ch != '\n') 
         s[i++] = ch; 

    s[i] = '\0'; 
} 

/*
  
run:
   
abcdef
s = abcdef

*/

 



answered Oct 26, 2015 by avibootz
...