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.
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.