/** * @Comment: WSU - Fall 06 - CS 3830-0 * @Title: prj2b.c - Project 2b * @Author: Vincenzo Maggio */ #include #include #include #include #include #include static struct myTime { clock_t timeEnter; clock_t timeExit; } userTime; void InitScreen ( void); void Menu ( void); void Rulers ( void); void UpdateTime ( void); void Wait4userinput ( void); /**/ void Mygets ( void); void Mystrcpy ( void); void Mystrcat ( void); void Mysprintf ( void); void Myscanf ( void); void UpdateTime( void) { struct tm *pMyTime; time_t aclock; time( &aclock); pMyTime = localtime( &aclock); gotoxy( 1,1); printf( "%s", asctime( pMyTime) ); } void InitScreen( void) { system("cls"); system("color f0"); } void Menu( void) { system("cls"); gotoxy( 27, 2); puts("--==]~~~ \01 0xYg3N \01 ~~~[==--"); puts("\n A. Program 1"); puts(" 1. gets() 2. strcpy()"); puts(" 3. strcat() 4. sprintf()"); puts(" 5. scanf() 0. Exit"); printf( "\n\n Enter your choice: "); } void Wait4userinput( void) { while ( !kbhit()) UpdateTime(); } void Rulers( void) { gotoxy( 13, 16); printf( "0 1 2 3 4 5 6 7 8 9 A B C D E F\n"); } void Mygets( void) { static char string[8]; static char saveMe[16]; int i; memset( string, '\0', 8); memset( saveMe, '\0', 8); for ( i = 0; i < 15; i++) saveMe[i] = *( string + i); gotoxy( 2, 12); printf( "gets() - Buffer is char[8+1], enter a string (8 < len < 16) chars long: "); gotoxy( 5, 14); printf( "String: "); Wait4userinput(); gotoxy( 13, 14); fgets( string, 9, stdin); Rulers(); gotoxy( 2, 18); printf("Before: "); for ( i = 0; i < 16; i++) printf( "%2x ", *(saveMe + i) ); gotoxy( 2, 19); printf("After : "); for ( i = 0; i < 16; i++) printf( "%2x ", *(string + i) ); gotoxy( 33, 20); printf("%c\n", 24); printf(" String correctly stops here __| only the first 8 chars are taken"); printf("\n\n Hit \"any\" key to continue..."); Wait4userinput(); getch(); } void Mystrcpy( void) { static char userInput[9]; static char dest[9]; int i; memset( userInput, '\0', 9); memset( dest, '\0', 9); gotoxy( 2, 12); printf( "strcpy() - Buffer is [8+1] - Enter a string of any length: "); gotoxy( 2, 14); printf( "Password: "); Wait4userinput(); gotoxy( 12, 14); fgets( userInput, 9, stdin); Rulers(); gotoxy( 2, 18); printf("Before: "); for ( i = 0; i < 16; i++) printf( "%2x ", *(dest + i) ); strncpy( dest, userInput, 8); gotoxy( 2, 19); printf("After : "); for ( i = 0; i < 16; i++) printf( "%2x ", *(dest + i) ); gotoxy( 33, 20); printf("%c\n", 24); printf(" String correctly stops here __| only the first 8 chars are taken"); printf("\n\n Hit \"any\" key to continue..."); Wait4userinput(); getch(); } void Mystrcat( void) { static char userInput[10]; static char saveMe[17]; static char dest[15]; int i; memset( userInput, '\0', 10); memset( saveMe, '\0', 17); memset( dest, '\0', 15); strcat( dest, "Edward" ); strcat( dest, " " ); for ( i = 0; i < 17; i++) saveMe[i] = *( dest + i); gotoxy( 2, 12); printf( "strcat() - Buffer fullName is [14+1], first name already in is Edward\n input lastName at least 8 chars long: "); gotoxy( 2, 14); printf( "Last Name: "); Wait4userinput(); gotoxy( 13, 14); fgets( userInput, 10, stdin); strncat( dest, userInput, 7); Rulers(); gotoxy( 2, 18); printf("Before: "); for ( i = 0; i < 16; i++) printf( "%2x ", *(saveMe + i) ); gotoxy( 2, 19); printf("After : "); for ( i = 0; i < 16; i++) printf( "%2x ", *(dest + i) ); gotoxy( 52, 20); printf("%c\n", 24); gotoxy( 20, 21); printf(" String correctly stops here __|"); printf("\n\n Hit \"any\" key to continue..."); Wait4userinput(); getch(); } void Mysprintf( void) { static char userInput[13]; static char lastName[11]; static char saveMe[17]; int i; memset( userInput, '\0', 13); memset( lastName, '\0', 11); memset( saveMe, '\0', 17); for ( i = 0; i < 16; i++) saveMe[i] = *( lastName + i); gotoxy( 2, 12); printf( "sprintf() - Last name is [10+1] chars long,\n input a string at least [11] long"); gotoxy( 2, 14); printf( "Last Name: "); Wait4userinput(); gotoxy( 13, 14); fgets( userInput, 12, stdin); strncpy( lastName, userInput, 10); Rulers(); gotoxy( 2, 18); printf("Before: "); for ( i = 0; i < 16; i++) printf( "%2x ", *(saveMe + i) ); gotoxy( 2, 19); printf("After : "); for ( i = 0; i < 12; i++) printf( "%2x ", *(lastName + i) ); gotoxy( 40, 20); printf("%c\n", 24); gotoxy( 8, 21); printf(" String correctly stops here __|"); printf("\n\n Hit \"any\" key to continue..."); Wait4userinput(); getch(); } void Myscanf( void) { static char dest[11]; static char saveMe[17]; int i; memset( dest, '\0', 11); memset( saveMe, '\0', 17); for ( i = 0; i < 17; i++) saveMe[i] = *( dest + i); gotoxy( 2, 12); printf( "scanf() - Buffer is [10+1]\n Insert a string (12 < len < 17) chars long"); gotoxy( 2, 14); printf( "Last name: "); Wait4userinput(); gotoxy( 13, 14); fgets( dest, 11, stdin ); Rulers(); gotoxy( 2, 18); printf("Before: "); for ( i = 0; i < 16; i++) printf( "%2x ", *(saveMe + i) ); gotoxy( 2, 19); printf("After : "); for ( i = 0; i < 16; i++) printf( "%2x ", *(dest + i) ); gotoxy( 40, 20); printf("%c\n", 24); gotoxy( 8, 21); printf(" String correctly stops here __|"); printf("\n\n Hit \"any\" key to continue..."); Wait4userinput(); getch(); } /* ---------------------------------- */ void main( ) { char userChoice; userTime.timeEnter = clock(); InitScreen(); LOOP: Menu(); /* trap */ Wait4userinput(); gotoxy( 21, 10); userChoice = getche(); switch ( userChoice ) { case '1': Mygets(); break; case '2': Mystrcpy(); break; case '3': Mystrcat(); break; case '4': Mysprintf(); break; case '5': Myscanf(); break; case '0': goto EXIT; default: gotoxy( 2, 12); printf("Error: wrong value\n Make a new choice\n\n"); system("pause"); goto LOOP; } goto LOOP; EXIT: userTime.timeExit = clock(); system("color 0f"); system("cls"); printf("\n You have been connected for %g seconds", ((userTime.timeExit - userTime.timeEnter) / (double)1000)); printf("\n Thanks for using Prj2b!\n\n"); exit( 0); }