IPC144 - Introduction to Strings Agenda: 1 Review/Questions 2 Array of characters 3 Character Strings 4 Try it! 5 Homework Array of Characters array of characters is an array, each element of which has a character data type. Example 1: #declare SIZE 4 /* declaring an array of characters */ char name[size]; /* we can assign a value to each element */ name[0] = F ; name[1] = r ; name[2] = e ; name[3] = d ; Example 2: Array of characters can be initialized at the same time when it is declared: #declare SIZE 4 /* Reminder: braces can be used only when initializing an array at the declaration time. */ char name[size] = { F, r, e, d ; /* then we can display every element using for loop: */ for( i = 0; i < SIZE; i ++) printf( %c, name[i]);
Example 3: /* fun27.c - using character arrays */ #include <stdio.h> #define SIZE 10 main(){ int i, flag = 1, length; char name[size]; printf("please enter your name (maximum %d characters): ", SIZE); for(i = 0; i < SIZE && flag; i++){ scanf("%c", &name[i]); if(name[i] == '\n') flag = 0; length = i < SIZE? i - 1:i; printf("your name is: "); for( i = 0; i < length ; i++ ) printf("%c", name[i]); printf("\n"); Strings String is an array of characters terminated by null ( \0 ) character. char name[5] = { F, r, e, d, \0 ; /* can t use this method after declaration */ /* NOTE: null character stored as a character, although it is not printable character */ Another way to declare a string is: char name[5] = Fred ; /* can t use this method after declaration */ /* the null character will be placed at the end of the string by default */ Now when we know that there is a null character at the end of the string, we can read a string up to \0 ( null character ).
Example 1: char product_name[31]= ifeel Mouse ; char product_desc[41]= Optical Mouse ; int i; for(i = 0; product_name[i]!= \0 ; i ++) printf( %c, product_name[i]); for(i = 0; product_desc[i]!= \0 ;i++) printf( %c, product_desc[i]); Input and output of strings %s is a format specifier used to output the string using printf or read the string of characters and store it into a string using scanf. char product_desc[41]; printf( Enter product description: ); /* scanf() will read input from the keyboard up to a white space character! That means that only one word will be stored into array, and it is up to a programmer to make sure that the array size is large enough to hold it. */ scanf( %s, product_desc); /* NOTE: name of the array is a pointer to the first element in the array, so it s value is an address of the first element in the array. When using scanf(), the name of the array is supplied to scanf() function. */ printf( Product description: %s\n, product_desc); #include <stdio.h> int main(){ int i, flag = 1, length; char first_name[21]; char last_name[21]; printf("please enter your first name (maximum 20 characters): "); scanf("%20s", first_name); printf("please enter your last name (maximum 20 characters): "); scanf("%20s", last_name); printf("first Name: %s\n", first_name); printf("last Name: %s\n", last_name); return 0;
Passing arrays to functions Recount that the name of the array is a pointer to the first element in the array, so it s value is an address of the first element in the array. When passing an array to a function the name of the array is passed (an address of the first element of the array!). In other words, the array is passed to a function using call by reference. void get_name( char arr[ ] ); int main(){ char name[5]; get_name(name ); /* passing array to the function */ printf( %s\n, name); return 0; void get_name(char arr[ ]){ arr[0] = J ; arr[1] = o ; arr[2] = h ; arr[3] = n ; arr[4] = \0 ; When an array is processed by a function, we need to know where the array ends. So, if it is an array of integers or doubles we must supply the length of the array to the function. If it is a string ( recount: a character array terminated by \0 character), the null character serves to mark the end of the string. /* displays a string character by character */ void display_name( char arr[ ] ){ int i; for( i = 0; arr[ i ]!= \0 ; i ++) printf( %c, arr[ i ]);
More scanf() (see example: fun36.c ) %[] - an additional format specifier available for scanf is %[] where the square brackets contain a list of valid characters. "-" character inside the square [] brackets indicates a range. "^" character inside the square [] brackets indicates that all characters should be accepted except those inside the square brackets. %30s number specifies maximum number of characters to be read. Examples: The following statement will accept any number of y's or n's scanf("%[ a-za-z0-9]", name); The following statement will accept only spaces, alphabetic characters, and digits. scanf("%[ a-za-z0-9]", name); The following statement will accept everything except a newline character. It will accept a line of input (including whitespace other than the newline) into a string. scanf("%[^\n]", line); up to 4 characters will be stored in the "word", any character will be accepted excluding whitespace characters scanf("%4s", word);
Try it! Write a function that will get a string and an integer as parameters. This function should truncate string for the number received as the second parameter. If the integer is less than 0 or greater than the size of the sting, the function should return 1. Otherwise the function returns 0. Here is the function prototype: int truncate(char s[], int num); Homework #8 (email your solution: arta.kogan@senecac.on.ca) Write a function called check_passwd() that will compare two strings. It takes two strings as parameters, and returns a character. If the two strings are equal then A returned, otherwise the first character that is different from the first string will be returned. For example: char ch; char passwd[15] = abcdefg ; char temp[15] = abcdefg ; ch = check_passwd(passwd, temp); /* ch will get a value of A */ ch = check_passwd(passwd, abcefgh ); /* ch will get a value of d */