Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.

Similar presentations


Presentation on theme: "1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string."— Presentation transcript:

1 1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string "Hello" is represented as: A string is accessed via a pointer to its first character. 'H''e''l' 'o''\0'

2 2 C-strings Declaring an initializing strings Using arrays: char message[] = {'H', 'i', '!', '\0'}; char message[] = {"Hi!"}; char message[10]; cin >> message; // CAREFUL: make sure the array is // large enough for the message Using pointers const char *message = "Hi!"; char *message = new char[10]; cin >> message; // CAREFUL: make sure the array is // large enough for the message

3 3 The cstring library int strlen (const char *str); Returns the length of string str, without counting the null character. Example: Code snippet: char *message = "Bye"; int size = strlen(message); for (int i = 0; i < size; i++) cout << *(message+i) << endl; Execution: [tlab-10]./a.out B y e [tlab-10]

4 4 The cstring library char *strcpy (char *dest, const char *src); Copies string src into string dest and returns the modified dest. CAREFUL: dest must be large enough to fit src. Example: Code snippet: char *dest = new char[20]; strcpy(dest, "Hello"); cout << dest << endl; char *src = "Bye"; strcpy(dest, src); cout << dest << endl; Execution: [tlab-10]./a.out Hello Bye [tlab-10] dest in memory (after second strcpy): 'B''y''e''\0''l''o''\0'... ? generally undefined

5 5 The cstring library char *strncpy (char *dest, const char *src, int n); Similar to strcpy but only copies first n characters of src into dest. CAREFUL: make sure dest is null-terminated after strncpy. If n > strlen(src), the remainder of dest is padded with nulls. Code snippet: char *dest = new char[20]; strcpy(dest, "Hello"); cout << dest << endl; char *src = "Yap"; strncpy(dest, src, 2); cout << dest << endl; dest[2] = '\0'; cout << dest << endl; strncpy(dest, "Haha", 15); cout << dest << endl; Execution: [tlab-10]./a.out Hello Yallo Ya Haha [tlab-10]

6 6 The cstring library char *strcat (char *str1, const char *str2); Appends str2 to str1 (overwriting str1's null character), appends a null character at the end and returns the modified str1 CAREFUL: make sure str1 is large enough. Example: Code snippet: char *dest = new char[20]; strcpy(dest, "Hello"); cout << dest << endl; char *src = " there"; strcat(dest, src); cout << dest << endl; Execution: [tlab-10]./a.out Hello Hello there [tlab-10]

7 7 The cstring library char *strncat (char *str1, const char *str2, int n); Similar to strcat but only appends first n characters of str1 to str2. It again adds a null character at the end. Example: Code snippet: char *dest = new char[20]; strcpy(dest, "Hello"); cout << dest << endl; char *src = " there"; strncat(dest, src, 4); cout << dest << endl; Execution: [tlab-10]./a.out Hello Hello the [tlab-10]

8 8 The cstring library int strcmp (const char *str1, const char *str2); Compares str1 with str2 and returns an integer less than, equal to or larger than 1 id str1 is less than, equal to or greater than str2. The function compares the strings character by character until it finds two different characters. It returns a value based on which character has a higher ASCII value. Example: Code snippet: cout << strcmp("", "blue") << endl; cout << strcmp("purple", "orange") << endl; cout << strcmp("Ha", "Ha") << endl; cout << strcmp("Ho", "ho") << endl; Execution: [tlab-10]./a.out 1 0 1 [tlab-10]

9 9 The cstring library int strncmp (const char *str1, const char *str2, int n); Similar to strcmp but compares only the first n characters. Example: Code snippet: cout << strncmp("purple", "orange", 1) << endl; cout << strncmp("apple", "apply", 4) << endl; cout << strncmp("hem", "hem", 10) << endl; Execution: [tlab-10]./a.out 1 0 [tlab-10]

10 10 The cstring library char * strtok (char *str1, const char *delims ); This function can be used to break str1 into tokens (non-empty subsets of str1 that do not contain any of the characters in delims). The first call to strtok should have str1 as its first argument. Subsequent calls should replace this with NULL. Each call returns a pointer to the next token, or NULL if no other tokens are found. delims may be different in each call. How it works: The last character of each token is a delimiter, but this is overwritten by a null character. A pointer to the next character is saved for the next call to strtok.

11 11 The cstring library char * strtok (char *str1, const char *delims ); Example Code snippet: char message[] = "Say the not-so-secret password"; char *tokenPtr; tokenPtr = strtok(message, " -"); while (tokenPtr != NULL) { cout << tokenPtr << endl; tokenPtr = strtok(NULL, " -"); } Execution: [tlab-10]./a.out Say the not so secret password [tlab-10] if you put message here instead, the loop will print Say over and over (i.e. it will not terminate).

12 12 The cstring library char * strchr (const char *str, char c ); Returns a pointer to the first occurrence of character c in str, NULL if the character does not appear in the string. Example Code snippet: char *ptr; if ( ptr = strchr ("apple", 'p') ) cout << ptr << endl; if ( !(ptr = strchr ("apple", 'b')) ) cout << "b is not in apple\n"; Execution: [tlab-10]./a.out pple b is not in apple [tlab-10]

13 13 The cstring library char *strstr (const char *haystack, const char *needle); Returns a pointer to the first occurrence of string needle in string haystack, NULL if the substring is not found. If needle is empty, the function returns haystack Example Code snippet: char *ptr; if ( ptr = strstr ("attempted", "") ) cout << ptr << endl; if ( ptr = strstr ("attempted", "te") ) cout << ptr << endl; if ( ptr = strstr ("attempted", "ted") ) cout << ptr << endl; if (!(ptr = strstr ("pearl", "par")) ) cout << "par is not in pearl\n"; Execution: [tlab-10]./a.out attempted tempted ted par is not in pearl [tlab-10]


Download ppt "1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string."

Similar presentations


Ads by Google