Thread: Custom gets and puts functions

  1. #1
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Question Custom gets and puts functions

    Hi,
    I am writing custom gets() and puts() functions. I have started like this but got stuck as i didnt know to store the char.... I think the code will tell you where i am stuck

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void xgets(char *str);
    
    char str1[100];
    
    int main()
    {
    	char str[100];
    
    	printf("Enter a string");
    	xgets(str);
    	printf("%s",str);
    
    	return 0;
    
    }
    
    void xgets(char *str)
    {
    	while(*str!='\n')
    	{
    well that was for gets

    and this is for puts

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void xputs(char *str);
    
    
    int main()
    {
    	char str[100];
    
    	printf("Enter a string");
    	gets(str);
    	xputs(str);
    
    
    	return 0;
    
    }
    
    
    void xputs(char *str)
    {
    	while(*str!='\0')
    	{
    		printf("%c",*str);
    		str++;
    	}
    }
    this one works if a string is passed to puts. But if puts is like this then i dont know how to get it working.

    puts("Print this");

    Hope i am clear with my question.

    Please help.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I'm not sure what the problem really is here. Is this the fix?
    Code:
    void xputs (const char * putMe)
    {
     while (*putMe != '\0')
     {
      ...
    Incidentally, puts prints a newline after printing the string.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    0x01
    Join Date
    Sep 2001
    Posts
    88

    Hmm... Has some problems ... try it anyway.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h> // getche() ... you have to get input from the keyboard some how
    
    #define ENTER     0x0D  // Enter Key
    #define BACKSPACE 0x08  // Backspace key
    #define TRUE        1
    #define FALSE       0
    
    void xputs(char *str)
    {
    	int idx = 0;
    
    	printf("\n");
    
    	while(str[idx] != NULL)
    	{
    		printf("%c",str[idx]); // take the * off
    		idx++;
    	}
    }
    
    void xgets(char * str)
    {
    	int idx = 0, i = 0;
    
    	//Get the number of chars/subscripts
    	while( str[idx] != NULL )
    		idx++;
    
    	// Force the chars/subscripts to be NULL
    	for( i = 0; i < idx; i++ )
    		str[i] = NULL;
    
    	// re/assign stuff here
    	idx = 0;
    
    	while(TRUE)
    	{
    		str[idx] = getche(); // getche() returns the key pressed
    
    		if(str[idx] == ENTER)
    			break;
    
    		idx++;
    	}
    }
    
    int main()
    {
    	char str[100];
    
    	printf("Enter a string : ");
    	//gets(str); printf("%s",str);
    
    	xgets(str);
    
    	// 1st way of puts()
    	xputs(str); 
    
    	// 2nd way of puts()
    	xputs("My puts function");
    
    	return 0;
    
    }
    There are some problems with this code, for i didn't spend much time with it. I hope this gives you some ideas.

Popular pages Recent additions subscribe to a feed