Using gets()/gets_s() to get a line from the stdin stream, putchar() to writes a character to stdout and reverse()

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Another function for reading from standard stream,  writing to standard output and then read the string character reversely using reverse()

To show: How to use the C functions gets()/gets_s(), putchar() and reverse() to read from standard stream, write a character to standard output and read character string reversely

 

 

// Using gets()/gets_s()/_getws() and putchar()/putwchar()

#include <stdio.h>

 

// function prototype...

void reverse(char *);

 

int main(void)

{

// an array for storing the string...

char sentence[80];

 

printf("Using gets() and putchar()\n");

printf("--------------------------\n");

 

// prompt for user input...

printf("Enter a line of text:\n");

// gets(sentence); - using a secure version

gets_s(sentence, 80);

printf("\nThe line printed backward is:\n");

// reverse() function call...

reverse(sentence);

printf("\n");

return 0;

}

 

void reverse(char *s)

{

// test if nothing entered...

if(s[0] == '\0')

return;

// if something entered...

else

{

reverse(&s[1]);

putchar(s[0]);

}

}

 

Output example:

 

Using gets() and putchar()

--------------------------

Enter a line of text:

This is a line of test text

The line printed backward is:

txet tset fo enil a si sihT

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index