










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The course covers important and advance elements of C and C plus plus programming language. This course provides the student with the skills required to design, code, and test and execute programs of simple to intermediate complexity. It includes: Standard, Input, Output, Library, Functions, Program, Output, Manipulation, String, Handling
Typology: Slides
1 / 18
This page cannot be seen from the preview
Don't miss anything!
int getchar( void ); (^) Inputs the next character from the standard input and re-
turns it as an integer.
char *gets( char *s ); (^) Inputs characters from the standard input into the array s
until a newline or end-of-file character is encountered. A terminating null character is appended to the array. int putchar( int c ); (^) Prints the character stored in (^) c. int puts( const char *s ); (^) Prints the string s followed by a newline character. int sprintf( char *s, const char *format, ... ); Equivalent to^ printf, except the output is stored in the array s instead of printing it on the screen. int sscanf( char *s, const char *format, ... );
Equivalent to scanf, except the input is read from the array s instead of reading it from the keyboard.
fig08_13.c (Part 1 of 2)
1 /* Fig. 8.13: fig08_13.c 2 Using gets and putchar / 3 #include <stdio.h> 4 5 int main() 6 { 7 char sentence[ 80 ]; / create char array / 8 9 void reverse( const char * const sPtr ); / prototype / 10 11 printf( "Enter a line of text:\n" ); 12 13 / use gets to read line of text / 14 gets( sentence ); 15 16 printf( "\nThe line printed backwards is:\n" ); 17 reverse( sentence ); 18 19 return 0 ; / indicates successful termination / 20 21 } / end main */ 22
fig8_14.c
1 /* Fig. 8.14: fig08_14.c 2 Using getchar and puts / 3 #include <stdio.h> 4 5 int main() 6 { 7 char c; / variable to hold character input by user / 8 char sentence[ 80 ]; / create char array / 9 int i = 0 ; / initialize counter i / 10 11 / prompt user to enter line of text / 12 puts( "Enter a line of text:" ); 13 14 / use getchar to read each character / 15 while ( ( c = getchar() ) != '\n') { 16 sentence[ i++ ] = c; 17 } / end while / 18 19 sentence[ i ] = '\0'; 20 21 / use puts to display sentence / 22 puts( "\nThe line entered was:" ); 23 puts( sentence ); 24 25 return 0 ; / indicates successful termination / 26 27 } / end main */
Program Output
fig08_16.c
Program Output
1 /* Fig. 8.16: fig08_16.c 2 Using sscanf / 3 #include <stdio.h> 4 5 int main() 6 { 7 char s[] = "31298 87.375"; / initialize array s / 8 int x; / define x / 9 double y; / define y / 10 11 sscanf( s, "%d%lf", &x, &y ); 12 13 printf( "%s\n%s%6d\n%s%8.3f\n", 14 "The values stored in character array s are:", 15 "integer:", x, "double:", y ); 16 17 return 0 ; / indicates successful termination / 18 19 } / end main */
Function prototype Function description
*s1, const char *s2 ) Copies string^ s2^ into array^ s1. The value of^ s1^ is returned.
Copies at most n characters of string s2 into array s1. The value of s is returned.
*s1, const char *s2 ) Appends string terminating null^ s2 character of^ to array^ s1. The first character ofs1. The value of s1 is returned.^ s2^ overwrites the
Appends at most n characters of string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.
fig08_19.c
1 /* Fig. 8.19: fig08_19.c 2 Using strcat and strncat / 3 #include <stdio.h> 4 #include <string.h> 5 6 int main() 7 { 8 char s1[ 20 ] = "Happy "; / initialize char array s1 / 9 char s2[] = "New Year "; / initialize char array s2 / 10 char s3[ 40 ] = ""; / initialize char array s3 / 11 12 printf( "s1 = %s\ns2 = %s\n", s1, s2 ); 13 14 / concatenate s2 to s1 / 15 printf( "strcat( s1, s2 ) = %s\n", strcat( s1, s2 ) ); 16 17 / concatenate first 6 characters of s1 to s3. Place '\0' 18 after last character / 19 printf( "strncat( s3, s1, 6 ) = %s\n", strncat( s3, s1, 6 ) ); 20 21 / concatenate s1 to s3 / 22 printf( "strcat( s3, s1 ) = %s\n", strcat( s3, s1 ) ); 23 24 return 0 ; / indicates successful termination / 25 26 } / end main */
Program Output
fig08_21.c
1 /* Fig. 8.21: fig08_21.c 2 Using strcmp and strncmp */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main() 7 { 8 const char s1 = "Happy New Year"; / initialize char pointer */ 9 const char s2 = "Happy New Year"; / initialize char pointer */ 10 const char s3 = "Happy Holidays"; / initialize char pointer / 11 12 printf("%s%s\n%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n\n", 13 "s1 = ", s1, "s2 = ", s2, "s3 = ", s3, 14 "strcmp(s1, s2) = ", strcmp( s1, s2 ), 15 "strcmp(s1, s3) = ", strcmp( s1, s3 ), 16 "strcmp(s3, s1) = ", strcmp( s3, s1 ) ); 17 18 printf("%s%2d\n%s%2d\n%s%2d\n", 19 "strncmp(s1, s3, 6) = ", strncmp( s1, s3, 6 ), 20 "strncmp(s1, s3, 7) = ", strncmp( s1, s3, 7 ), 21 "strncmp(s3, s1, 7) = ", strncmp( s3, s1, 7 ) ); 22 23 return 0 ; / indicates successful termination / 24 25 } / end main */
Program Output
fig08_23.c (Part 1 of 2)
1 /* Fig. 8.23: fig08_23.c 2 Using strchr */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main() 7 { 8 const char string = "This is a test"; / initialize char pointer / 9 char character1 = 'a'; / initialize character1 / 10 char character2 = 'z'; / initialize character2 / 11 12 / if character1 was found in string / 13 if ( strchr( string, character1 ) != NULL ) { 14 printf( "'%c' was found in "%s".\n", 15 character1, string ); 16 } / end if / 17 else { / if character1 was not found / 18 printf( "'%c' was not found in "%s".\n", 19 character1, string ); 20 } / end else */ 21
fig08_23.c (Part 2 of 2)
Program Output
22 /* if character2 was found in string / 23 if ( strchr( string, character2 ) != NULL ) { 24 printf( "'%c' was found in "%s".\n", 25 character2, string ); 26 } / end if / 27 else { / if character2 was not found / 28 printf( "'%c' was not found in "%s".\n", 29 character2, string ); 30 } / end else / 31 32 return 0 ; / indicates successful termination / 33 34 } / end main */