CP264 Lecture 8 Strings PDF
Document Details
![HaleNeodymium](https://quizgecko.com/images/avatars/avatar-10.webp)
Uploaded by HaleNeodymium
Wilfrid Laurier University
Tags
Summary
These lecture notes cover the concepts of strings in C programming, exploring string pointers, operations, and library functions. The document provides examples and implementation details for understanding and working with strings in C, including array initialization, traversal, and comparison. The content is suitable for undergraduate computer science students.
Full Transcript
Lecture 8 Strings 1. Concepts of strings 2. String pointers 3. String operations 4. String library functions 1. Concepts of Strings Concepts of strings in C 1. A string is a sequence of characters terminated with the null character stored in a char array. 2. Char array is used to hold a st...
Lecture 8 Strings 1. Concepts of strings 2. String pointers 3. String operations 4. String library functions 1. Concepts of Strings Concepts of strings in C 1. A string is a sequence of characters terminated with the null character stored in a char array. 2. Char array is used to hold a string. Each character is stored in an array position one after another. The end of string is marked by the null character ‘\0’ Note: the ASCII value of ‘\0’ is 0. NULL as pointer value of 0. 3. The length of a string is the number of non-null characters in the string. A string is represented and accessed by a char pointer pointing to the memory location of the first character. Example char str; // declare a char array of 10 bytes str = ‘H'; str = ‘e'; ste = ‘l'; str = ‘l'; str = ‘o'; str = ‘\0'; // this is the terminator printf(“%s”, str); // output: Hello char array str holds the string “Hello”. str presents the address of the string. The first five elements of array str hold characters 'H', 'e', 'l', 'l', 'o' The sixth element str holds the string terminator ‘\0'. The array elements str, str, str, str are not used. Array name str is a char pointer pointing to the first character. The length of the string str is 5 The length of the array str is 10. The size of the array str is 10. char array initialization by string char name[ ] = “cp264”; char name = {‘c’, ‘p’, ‘2’, ‘6’, ‘4’, ‘\0’}; The compiler allocates char array name of 6 elements (6 types), Assign values ‘c’, ‘p’, ‘2’, ‘6’, ‘4’, ‘\0’ to the char array elements. The length of the string that array name holds is 5. The size of the array name is 6. 2. String pointers Since a string is stored in a char array, char pointer and operations apply to string data operations. char name = “cp264”; char *p = &name; // or char *p = name; printf("%s\n", p); // output: cp264 printf("%c\n", *p); // output: c printf("%c\n", *(p+3)); // output: 6 printf("%s\n", p+2); // output: 264 *p = ‘C’; *(p+1) = ‘P’; printf("%s\n", p); // output: CP264 p is char type pointer and char type has 1 byte, p+1 increase the value of p by 1. String pointer char *p = "cp264"; printf("%s\n", p); // output: cp264 printf("%c\n", *(p+2)); // output: 2 When a string is declared like char *p = “cp264”; Then compiler will allocate read-only memory char array to hold string “cp264”, and char pointer p pointers to the array, so operations like *(p+1) = ‘P’; is not allowed. String traversal void display_string(char s[]) { char *p = s; while( *p != ‘\0’) { printf(“%c”, *p); p++; } } char str[] = “cp264”; display_string(str) ; This print string: cp264 Since a string has a terminator, no need to pass the length of a string to a function. Other implementations of the display function void display_string(char *s) { char *p = s; while( *p != ‘\0’) { printf(“%c”, *p); p++; } } void display_string(char *s) { while( *s != ‘\0’) { printf(“%c”, *s); s++; } } void display_string(char *s) { while( *s ) { printf(“%c”, *s++); } } Array of strings An array of strings is a sequence of strings stored in a 2D char array, each row holds one string. char names = { "cp264“, “data structures II", "C programming language“ }; – This declares a 2D char array, it can hold 20 strings, each has maximum length 39. It also initializes the first three rows by the given string data. – names[i] is a pointer pointing to row i, ith string in names. printf("%s", names); // output: cp264 printf("%s", names); // output: data structures II printf("%s", names); // output: C programming language for (int i=0;i