Introduction to C Programming Ray Lauff Temple University Computer Science v2.0 NOTE: Not all the material covered in class is included in this slide deck. These slides are not meant to be used as a stand-alone learning tool. You are still responsible for ALL material covered. Get the notes from a classmate.
What is a string?
What is a string? A string is a collection of zero or more characters followed by the ASCII NUL character. '\0'
What is a character array?
What is a character array? A character array is a variable of type char that represents one or more characters. char my_char_array[ 400 ];
1. C will create strings for you automatically every time you use double quotes in source code. True or False?
1. C will create strings for you automatically every time you use double quotes in source code. True or False? char name[ 20 ] = "Patty Programmer";
2. Given: puts( "My brain is full!!!" ); The argument to the puts() function is a string. True or False?
3. Given: printf( "%s\n", "That does not compute." ); Both arguments to the printf() function are strings. True or False?
4. The puts( ) statement can only take one argument, and it must be a string. True or False?
puts() and printf() The puts( ) function ONLY accepts one string argument. The printf( ) function ALWAYS takes one string argument as the first parameter to the function. All the other arguments to printf( ) are dependent on this first argument. The other arguments to printf( ) to match up with the placeholders. Those other arguments can be of all different types, and are always passed by value to printf( ). Is printf( ) special or what??
5. The sprintf( ) function allows you to write strings to the screen just like the printf( ) function. True or False?
sprintf( ) Writes output to a string, not to the screen. The string is the first parameter to the function. This is the prototype for sprintf( ), which can be found in stdio.h. int sprintf(char * restrict str, const char * restrict format,...);
6. sprintf( ) should be used instead of snprintf( ) because it allows you to output the data to files as well as to strings. True or False?
snprintf( ) snprintf( ) is preferred because it limits the size of the data that goes into the string, preventing overindexing. const int bufsize = 80; char buffer[ bufsize ]; int yum = 700; char name[100] = "Big Rock Candy Mountain"; snprintf( buffer, bufsize, "%d %s", yum, name );
snprintf( ) This is the prototype for snprintf( ), which is found in stdio.h. int snprintf(char *str, size_t size, const char *format,...); Only the first three parameters are required. The placeholders in the format determine how many additional parameters are required on the snprintf( ) invocation.
Choose the BEST answer! 7. The primary difference between an array and a string is a) the length of an array is always known, but the length of a string varies. b) multiple strings can be stored in an array, but only one can be in a string. c) sizeof a will always return the number of elements in the array a. d) strings always end with a sentinel value where arrays may or may not. e) arrays can hold strings, but strings can't hold arrays. f) a string can't be passed to a function, but an array can.
Choose the BEST answer! 8. The length of a string can be determined with a) sizeof b) strlen c) strnlen d) sizeof / sizeof one of the elements
strlen and sizeof Remember that strlen returns the number of characters in a string. char name[ ] = "Tan"; strlen( name ); // will return 3
strlen and sizeof sizeof returns the space occupied by a variable. char name[ ] = "Tan"; sizeof name; // will return 4
strlen and sizeof sizeof is an operator and is "baked in" to C as part of the language. strlen is a function with a prototype provided in the string.h header. extern size_t strlen ( const char * s);
strlen and sizeof size_t strlen ( const char * s); What is the return type of this function prototype?
strlen and sizeof size_t strlen ( const char * s); size_t is a type suitable for representing the amount of space a given thing requires, expressed in characters. It is always going to be zero or a positive integer. It is the return type of both strlen and sizeof. It is a 'unsigned long integer'.
strlen and sizeof char name[ ] = "Tan"; unsigned long stringlen = strlen( name ); unsigned long sizelen = sizeof name; printf( "The length of the string is %lu.\n", stringlen ); printf( "The memory spaced used is %lu.\n", sizelen );
strlen and sizeof char name[ ] = "Tan"; unsigned long stringlen = strlen( name ); unsigned long sizelen = sizeof name; printf( "The length of the string is %lu.\n", stringlen ); printf( "The memory spaced used is %lu.\n", sizelen ); The length of the string is 3. The memory spaced used is 4.
9. char buf[ 12 ] = "Temple"; The number of elements in this array which have been initialized is a) 0 b) 6 c) 7 d) 8 e) 9 f) 11 g) 12
10. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf );
10. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf ); Temple
11. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%c\n", buf[0] );
11. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%c\n", buf[0] ); T
12. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%c\n", *buf );
12. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%c\n", *buf ); T
13. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%d\n", buf[0] );
13. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%d\n", buf[0] ); 84
14. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf[ 4 ] );
14. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf[ 4 ] ); Segmentation Fault! Core dump.
15. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf + 4 );
15. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf + 4 ); le
Note Doing the math with a pointer, as with char buf[ 12 ] = "Temple"; printf( "%s\n", buf + 4 ); can also be written as printf( "%s\n", &(buf[ 4 ])); or you can print just a single character as with printf( "%c\n", buf[ 4 ] );
16. What gets printed by the following? char buf[ 12 ] = "Temple"; int i = 3; while ( buf[i]!= '\0' ) putchar( buf[ i++ ] );
16. What gets printed by the following? char buf[ 12 ] = "Temple"; int i = 3; while ( buf[i]!= '\0' ) putchar( buf[ i++ ] ); ple
17. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf + 3 );
17. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf + 3 ); ple
18. What gets printed by the following? char buf[ 12 ] = "Temple"; int i; for ( i = 0; buf[ i ]!= '\0'; i++ ) if ( buf[ i ] == 'e' buf[ i ] == 'l' ) putchar( buf[ i ] );
18. What gets printed by the following? char buf[ 12 ] = "Temple"; int i; for ( i = 0; buf[ i ]!= '\0'; i++ ) if ( buf[ i ] == 'e' buf[ i ] == 'l' ) putchar( buf[ i ] ); ele
19. What gets printed by the following? char buf[ 12 ] = "Temple"; int i; for ( i = 0; buf[ i ]!= '\0'; i++ ) if ( buf[ i ] == 'e' buf[ i ] == 'l' ) putchar( buf[ i - 1 ] );
19. What gets printed by the following? char buf[ 12 ] = "Temple"; int i; for ( i = 0; buf[ i ]!= '\0'; i++ ) if ( buf[ i ] == 'e' buf[ i ] == 'l' ) putchar( buf[ i - 1 ] ); Tpl
20. What is in the variable hold? char buf[ 12 ] = "Temple"; char hold[ 100 ]; int i; int j = strlen( buf ); hold[ j-- ] = '\0'; for ( i = 0; buf[ i ]!= '\0'; i++ ) hold[ j-- ] = buf[ i ];
20. What is in the variable hold? char buf[ 12 ] = "Temple"; char hold[ 100 ]; int i; int j = strlen( buf ); hold[ j-- ] = '\0'; for ( i = 0; buf[ i ]!= '\0'; i++ ) hold[ j-- ] = buf[ i ]; The variable hold now contains: e l p m e T \0 [0] [1] [2] [3] [4] [5] [6]
21. What gets printed by the following? char *nicks[ ] = { }; "Crash Override", "Acid Burn", "Zero Cool", int i; int n = sizeof nicks / sizeof nicks[0]; for ( i = 0; i < n; i++ ) printf( "%c\n", nicks[ i ][ 1 ] );
21. What gets printed by the following? char *nicks[ ] = { }; "Crash Override", "Acid Burn", "Zero Cool", int i; int n = sizeof nicks / sizeof nicks[0]; for ( i = 0; i < n; i++ ) printf( "%c\n", nicks[ i ][ 1 ] ); r c e
22. What gets printed by the following? char *nicks[ ] = { "Crash Override", "Acid Burn", "Zero Cool", }; int i, j; int n = sizeof nicks / sizeof nicks[0]; for ( i = 0; i < n; i++ ) { for ( j = 0; nicks[ i ][ j ]!= '\0'; j++ ) putchar( nicks[ i ][ j ] ); putchar( '\n' ); }
22. What gets printed by the following? char *nicks[ ] = { "Crash Override", "Acid Burn", "Zero Cool", }; int i, j; int n = sizeof nicks / sizeof nicks[0]; for ( i = 0; i < n; i++ ) { for ( j = 0; nicks[ i ][ j ]!= '\0'; j++ ) putchar( nicks[ i ][ j ] ); putchar( '\n' ); } Crash Override Acid Burn Zero Cool
23. What gets printed by the following? char *nicks[ ] = { "Crash Override", "Acid Burn", "Zero Cool", }; int i, j; int n = sizeof nicks / sizeof nicks[0]; int sums[ 10 ]; for ( i = 0; i < n; i++ ) { sums[ i ] = summit( nicks[ i ] ); printf( "Sums [ %d ] = %d.\n", i, sums[i] ); } int total = 0; for ( i = 0; i < n; i++ ) total += sums[ i ]; printf( "Total = %d\n", total );
int summit( char *s ) { int i = 0; int count = 0; while ( s[ i ]!= '\0' ) { if ( s[ i ] == 'a' s[ i ] == 'e' s[ i ] == 'i' s[ i ] == 'o' s[ i ] == 'u' s[ i ] == 'y' ) count++; i++; } } return count;
Sums [ 0 ] = 4. Sums [ 1 ] = 2. Sums [ 2 ] = 4. Total = 10
Caesar Cipher Caesar Cipher is one of the most basic forms of an encryption algorithm. In this type of encryption, each letter in the message is replaced with a letter corresponding to a certain number of letters up or down in the alphabet.
Caesar Cipher For example, we can use the algorithm to convert Temple into Ufnqmf
Caesar Cipher For example, we can use the algorithm to convert into Temple Ufnqmf (Which is each letter in sequence have increased by 1 location in the alphabet. ASCII makes it easy!)
Caesar Cipher void encrypt( char arr[] ) { int i; } for( i = 0; i < strlen( arr ); i++ ) { arr[i] += ROT; }
Caesar Cipher void decrypt( char arr[] ) { int i; } for( i = 0; i < strlen( arr ); i++ ) { arr[i] -= ROT; }
#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char msg[100]; const int rot = 1; void encrypt( char arr[] ) { int i; for( i = 0; i < strlen( arr ); i++ ) { arr[i] += rot; } } void decrypt( char arr[] ) { int i; for( i = 0; i < strlen( arr ); i++ ) { arr[i] -= rot; } } } printf( "Enter your message: " ); fgets( msg, 100, stdin ); printf("\nencrypted Version\n"); encrypt(msg); printf("%s\n",msg); printf("\ndecrypted Version\n"); decrypt(msg); printf("%s",msg); puts(""); return EXIT_SUCCESS;
%./a.out Enter your message: Temple Encrypted Version Ufnqmf Decrypted Version Temple %./a.out Enter your message: Luke, I am your father! Encrypted Version Mvlf-!J!bn!zpvs!gbuifs" Decrypted Version Luke, I am your father! %
Moving On from Strings