1 / 13

C-strings

C-strings. String = null-terminated array of characters The null character ('') 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'. 'l'. 'o'. ''. C-strings.

urit
Download Presentation

C-strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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' 'l' 'o' '\0'

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

  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. 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. 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. 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. 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 0 if str1 is lexicographically 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: Execution: [tlab-10] ./a.out -1 1 0 1 [tlab-10] Code snippet: cout << strcmp("", "blue") << endl; cout << strcmp("purple", "zoo") << endl; cout << strcmp("Ha", "Ha") << endl; cout << strcmp("Ho", "ho") << endl;

  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: Execution: [tlab-10] ./a.out 1 0 0 [tlab-10] Code snippet: cout << strncmp("purple", "orange", 1) << endl; cout << strncmp("apple", "apply", 4) << endl; cout << strncmp("hem", "hem", 10) << endl;

  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. 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. 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. 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]

More Related