360 likes | 517 Views
Programming Lab (Lab5). Strings, Pointers and Tools. Overview. A string is a sequence of characters. It is a kind of data. A literal string is a sequence of characters enclosed by a pair of double quotes " " . A string is stored as an array of char .
E N D
Programming Lab (Lab5) Strings, Pointers and Tools
Overview • A string is a sequence of characters. It is a kind of data. • A literal stringis a sequence of characters enclosed by a pair of double quotes " ". • A string is stored as an array of char. • Strings are often processed using pointers. • The C standard library provides many useful string handling functions.
Basic String Concept: Literal String #include <stdio.h> int main(void) { printf("Hello, %s, morning!\n", "C Program"); return 0; } Hello, C Program, morning! NULL terminator is added automatically at the end of the literal string.
Basic String Concept: Literal String • A literal string is in fact a constant array of constant characters. • The location (address) as well as the content of the array is fixed, i.e. “hello”. This is similar to constant like 123. • Modifying the content of a literal string in run-time will CRASH the program! (See the exercise follows…)
Creation of Literal String • When we write a C program, a pair of bare double quotes represents a literal string, e.g., “hello”. • The compiler and the linker allocate memory for storing the literal string automatically , excluding the double quotes (optional). • Following the rule, there is an extra character reserved automatically and put right after the literal string content, the NULL (ASCII code 0) character. • The NULL terminator in fact signals the end of the string since C does NOT keep the length of the string! (strlen())
Revision: char Array • A char array can be initialized with • a sequence of characters (in pairs of single quotes); • a sequence of ASCII codes. • The array size can be specified explicitly OR implicitly. • The characters are stored consecutively. • The array content can be modified in run-time.
Basic String Concept: char Array NULL terminator is added manually. #include <stdio.h> int main(void) { charname[15] = {'C',' ','P','r','o','g','r','a','m','\0'}; chargreet[] = {109, 111, 114, 110, 105, 110, 103, 0}; printf("Hello, %s, %s!\n", name, greet); return 0; } Hello, C Program, morning! NULL terminator is added manually.
Char Array Usage Tips • The array size should be large enough for storing the character content (trivial!). E. g., char a[2]=“hello”; • We should reserve space and put the NULL terminator at the end of the string. E.g., manually: char name[15] = {'C',‘ ','P','r','o','g','r','a','m','\0'}; • Drawback: there may be unused space left if the declared array is too large.
Initializing a char Array with a Literal Initializing a char array (string variable) with a literal string (constant). #include <stdio.h> int main(void) { charname[15] = "C Program"; printf("Hello, %s, morning!\n", name); return 0; } Hello, C Program, morning! Note that there is already a NULL terminator in the literal string.
Initializing a char Array with a Literal • It looks like "String Copy." • This is in fact initialization of an array during variable declaration! • We CANNOT do assignment with an array.
Exercise #include <stdio.h> int main(void) { charname[15] = "C Program"; name[1] = '-'; name[6] = 0; printf("Hello, %s, morning!\n", name); return 0; } Hello, C-Prog, morning! // Modify the content of the array name
Exercise #include <stdio.h> int main(void) { char myName[15] = "C Program"; char yourName[30]; yourName = myName; printf("Hello, %s, morning!\n", yourName); return 0; } SYNTAX ERROR!!! // Whole Array Assignment!
Exercise #include <stdio.h> int main(void) { char * ptr = "C Program"; ptr[1] = '-'; ptr[6] = 0; printf("Hello, %s, morning!\n", ptr); return 0; } CRASH!!! // Modify the content of the literal string which is constant!
Exercise #include <stdio.h> int main(void) { charname[15] = "C Program"; char * ptr; ptr = name; // Equivalent: ptr = &name[0] // i.e. ptr points to name[0] ptr[1] = '-'; ptr[6] = 0; printf("Hello, %s, morning!\n", name); printf("Bye bye, %s!\n", ptr); return 0; } Hello, C-Prog, morning! Bye bye, C-Prog!
String Handling Functions • The standard library contains many useful string handling functions. • They all require that strings passed as arguments be NULL-terminated. and well-allocated. • Their function prototypes are given in the header file <string.h>.
Determining String Length size_t strlen( const char s[] ) • Returns the number of characters (not including the terminating NULL character '\0') in the string s char str1[10] = "ABC"; char str2[] = "This is a string!"; int len; len = strlen(str1); // coerce from size_t to int printf("%d\n", len); // prints 3 printf("%d\n", strlen(str2)); // prints 17 printf("%d\n", strlen("Hello")); // prints 5
Displaying Strings – puts() int puts( char s[] ) Print the string s followed by a newline character. What's the return value of puts()? char s[] = "This is a string!"; puts(s); // Same as printf("%s\n", s);
& & Getting Strings – scanf() #include <stdio.h> int main(void) { char s1[20], s2[20]; scanf("%s", s1); printf("s1=%s\n", s1); printf("\n"); scanf("%s", s2); printf("s2=%s\n", s2); return 0; } this-is-first-string. s1=this-is-first-string. this is second string. s2=this • When inputting a string using scanf(), the argument(s) does NOT need to be preceded by '&'. W H Y ? • White spaces (such as space, tab) are regarded as delimiters by scanf().
Getting Strings – gets() char * gets( char s[] ) Input characters from the standard input into the array s until a newline or the End-Of-File character is encountered. A terminating null character is appended to the array and the array s (i.e., the address of s[0]) is returned. char name[20]; printf("Your name? "); gets(name); // we ignore the return value printf("Hello %s!\n", name); Your name? C Program Hello C Program!
Copying strings char * strcpy( char s1[], const char s2[] ) • Copies s2 (including the NULL character) into s1 • We should ensure s1 be large enough to store s2 (including the NULL character) • String s1 (i.e. the address of s1[0]) is returned char s1[7] = "123456", s2[7] = "ABC"; strcpy(s1, s2); // Output ABC in s1 printf("%s\n", s1); s1 s2 s1 becomes Copy up to the NULL character. The remaining characters in s1 are untouched.
Copying strings (with an optional size) char * strncpy( char s1[], const char s2[], size_tn ) • If s2 has n or more characters, the function copies the first n characters from s2 into s1.NO NULL character is appended to s1. • If s2 has less than n characters, the function behaves like strcpy(). That is, it copies up to the NULL character.
strncpy() (Examples) char s1[7] = "123456", s2[7] = "ABC"; strncpy(s1, s2, 2); // output AB3456 printf("%s\n", s1); strncpy(s1, s2, 10); // output ABC printf("%s\n", s1); strncpy(s2, "12345", 4); // Warning! s2 may NOT be // NULL terminated! s1 s2 s1 becomes s1 becomes s2 becomes
Concatenating strings char * strcat( char s1[], const char s2[] ) • Appends s2 to s1. • First character of s2 replaces the terminating NULL character of s1. • We should ensure s1 is large enough to store the concatenated string and a NULL character. • s1 is returned, s2is untouched. char s1[10] = "ABC"; char s2[20] = "123456"; strcat(s1, s2); // s1 becomes "ABC123456" strcat(s2, "789"); // s2 becomes "123456789"
Comparing strings int strcmp( const char s1[], const char s2[] ) • Compares s1 and s2 character-by-character (up to the NULL character) based on their ASCII values • Returns Zero if both strings are equal A negative integer if s1 precedes (less than) s2 A positive integer if s1 succeeds (greater than) s2 char s1[10], s2[20]; scanf("%s %s", s1, s2); if ( strcmp(s1, s2) == 0 ) printf("Both strings are identical");
Comparing strings s1 s2 s1 s2 s1 s2 s1 s2 s1 s2
Other Useful Functions #include <stdlib.h> int atoi( const char str[] ) long atol( const char str[] ) • The functions retrieve the longest leading substring in str that represents a valid integer, and returns the equivalent value as a value of type int/ long. • Leading white-space characters are ignored. int a; a = atoi("1234"); // a becomes 1234 a = atoi(" \n\n3123"); // a becomes 3123 a = atoi("ABC10"); // a becomes 0 a = atoi("-1+1"); // a becomes -1 a = atoi("99.8e3"); // a becomes 99
Other Useful Functions #include <stdlib.h> double atof( const char str[] ) • The function retrieves the longest leading substring in str that represents a valid floating point number, and returns the corresponding value as a double. • Leading white-space characters are ignored. double x; x = atof("3.14"); // x becomes 3.14 x = atof(" \n\n3.4"); // x becomes 3.4 x = atof("ABC10"); // x becomes 0.0 x = atof("-.344"); // x becomes -0.344 x = atof("1.2e3"); // x becomes 1200.0 x = atof("1.4.4"); // x becomes 1.4
Remark • There are many other string handling functions in the standard library. • Check the documentation to see if an existing function suits your need before attempting to write your own string handling functions. • Remember, browsing through reference manuals is the essential technique that every C programmer should equip with.
Strings and Pointers #include <stdio.h> #include <string.h> int main(void) { char message[] = "Welcome to C!"; char *cptr; cptr = message + 11; printf("%s\n", cptr ); printf("%s\n", &message[11] ); return 0; } C! C!
Memory Demonstration message cptr cptr
Practice 1--Common Array • Two arrays are identical if they have the same elements in the same order. • Algorithm for testing if two arrays, A and B, are identical: • If both arrays have different number of elements, then the array are different. • Otherwise, compare elements between A and B one-by-one. If one of the elements is different, then the arrays are different. • If none of the elements between A and B are different, then the arrays are identical.
// Suppose A and B are two arrays of the same type // with n and m elements respectively. int isIdentical = 1; // Assume A and B are identical // this is a boolean FLAG (T/F) if (n != m) isIdentical = 0; // set the flag to FALSE else { int i; for (i = 0; i < n; i++) if (A[i] != B[i]) { isIdentical = 0; // set the flag to FALSE break; // stop the loop, save time! } } if (isIdentical) printf("A and B are identical.\n");
Practice 2– Substring Search • Locate a substring (s1) in a string (s2). • i.e., At which location does s1 first appear in s2? s1 s2 1st iteration s1 s2 ≠ 2nd iteration s1 matches a substring starts at 1 s1 s2 =
// Returns the location of “substr" in "str". // Returns -1 if "str" does not contain “substr". int locate( const char str[], const char substr[] ) { int i, j, len1, len2; len1 = strlen(str), len2 = strlen(substr); // For all possible starting locations for (i = 0; i <= (len1 – len2); i++) { int isIdentical = 1; // For each substring started at location i for (j = 0; j < len2; j++) if ( substr[j] != str[i+j] ) { isIdentical = 0; break; } if (isIdentical) // Return the 1st matching substr return i; } return -1; } Substring Search Version 1
Substring Search using strncmp() // Returns the location of “substr" in "str". // Returns -1 if "str" does not contain “substr". int locate( const char str[], const char substr[] ) { int i, j, len1, len2; len1 = strlen(str), len2 = strlen(substr); // For all possible starting locations for (i = 0; i <= (len1 – len2); i++) { if ( strncmp(substr, &str[i], len2) == 0 ) return i; } return -1; } Version 2 // Returns the address of the first occurrence of the // substring in the text. Returns NULL on not found. char * p; p = strstr("This is a text.", "is"); => p ≡ "is is a text." Substring Search using strstr() Version 3
Challenging Yourselves Now • Today you face a white board but you have the key parts. • No templates today !!!