I have written the inbuilt gets() and puts() function....
Can anyone help me to make it much more simpler...

Attachments
#include<stdio.h>
#include<conio.h>
void getstr(char *a)
{
     int q,i=0;
     while(i<100)
     {
     scanf("%c",&a[i]);
     q=a[i];
            if(q==10)
            {
            a[i]='\0';
            return;
            }
     i++;
     }
}
void strrev(char *a)
{
     if(*a!='\0')
                 strrev(a+1);
     printf("%c",*a);
}
void putstr(char *a)
{
     int i=0;
     for(i=0;i<100;i++)
     {
           if(a[i]=='\0')
                        return;
            else
               printf("%c",a[i]);  
     }
}
int main()
{
    char a[20];
    printf("\nEnter the line:");
    getstr(a);
    printf("\nThe entered line is  ");
    putstr(a);
    printf("\nThe reversed line is  ");
    strrev(a);
    getch();
    return 0;
}

The getstr function is dodgy. scanf() is looking to take in just one char, but the user is trying to enter a string. And scanf() has to have an enter key to assign a value (buffered input).

fgets() is the function that immediately comes to mind for string entry. Overflowing the buffer array is no problem, and only one enter key needs to be pressed for each line. Also, the enter key is removed from the keyboard buffer with fgets(), and the end of string marker char is automatically added for you, if you just leave a space for it in the buffer.

fgets() for the win, imo.

Post back if you need help with fgets().

Here the user s goin to enter a line....and my getstr() ll get t letter by letter and make t as an array.....
Can u explain me the thing detaily....
I am just a beginner...

Edited 6 Years Ago by snivas519: n/a

fgets() is a very good standard C way, to get a line of char's. You need a char buffer, where fgets() will put the chars, and you need some little bit of code:

char buffer[100];  //make it bigger than any line should be
fgets(buffer, sizeof(buffer), stdin);

sizeof() just tells fgets() it can get UP TO 100 char's and no more, from the file stream named stdin. In practice, it should get no more than 98 char's from the user, because it will need two spaces: one for the newline when the user hits the enter key, and one for the end of string marker char: '\0' (which is a zero in value).

stdin is a file that C opens every time it runs a program - along with stdout. stdin is the keyboard, and stdout is your monitor.

ya....tats so good....And i have learnt about stdin and stdout files too...
But where ll we use this stdout file....

Good to have u as my friend....I am interested n doin a project in c...
I have finished half of t...my project s ubuntu terminal...
I have to include a feature in which if we press up arrow t has to go to the previous commands....Can you give me a idea on that...

This article has been dead for over six months. Start a new discussion instead.