L13 - strings - Strings, Strings, Strings We've seen...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Strings, Strings, Strings • We’ve seen several ways of handling strings in C++: – null terminated arrays of characters (C-style strings) – the standard C++ string class – the String class in Visual C++ 2005 • See Ch. 8 of Savitch for C-strings and string class; Ch. 5 of the C++/CLI text for String class • Just so we understand the similarities and differences, we’ll spend some time today talking about these • Conceptually, a string is a series of characters that is treated as a single unit
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
C-style strings a C-style string in C++ is an array of characters ending in the null character \0 – the null character indicates where in memory the string terminates the length of the string (i.e. the number of elements of the array) is (# of characters in the string) + 1 – add one for the null character terminator this kind of string is accessed via a pointer to the first character in the string the value of the string is generally regarded as the (constant) address of its first character this is why (when we are referring to strings in this mode) we talk about a string as being a constant pointer
Background image of page 2
C-style strings in C++ (cont) • examples: char city[] = “Dallas”; // creates a 7 element array city with elements ‘D’ ‘a’ ‘l’ ‘l’ ‘a’ ‘s’ ‘\0’ const char *cityPtr = “Dallas”; // creates a pointer that points to the letter D in the string “Dallas” stored somewhere in memory • failure to allocate enough memory in a character array to store the null character at the end of a string is an error!! • we can read data into a string using cin: – we’d declare a character array name[20], for example – then assign a value via cin >> name; – as long as the string of data is not longer than 19 characters, we are fine
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Library functions for C-style strings char *strcpy (char *s1, const char *s2); copies the string s2 into the char array s1; a pointer to s1 is returned char *strncpy (char *s1, const char *s2, size_t n); copies at most n chars of s2 into the char array s1; a pointer to s1 is returned char *strcat (char *s1, const char *s2); appends the string s2 to the string s1(overwriting s1’s \0); a pointer to s1 is returned char *strncat (char *s1, const char *s2, size_t n); appends at most n characters of s2 to s1; a pointer to s1 is returned int strcmp (const char *s1, const char *s2); compares s1 with s2; 0 if s1 =s2 ; <0 if s1 < s2 ; >0 if s1 > s2 int strncmp (const char *s1, const char *s2, size_t n); same as strcmp but comparison limited to first n characters char *strtok (char *s1, const char *s2); a sequence of calls to strtok breaks s1 into “tokens” – logical pieces – delimited by characters in s2 – see documentation about this size_t strlen (const char *s); determines the length of the string s (excluding the null character)
Background image of page 4
Comments on C-style strings #include <cstring> to make sure that the names declared with
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/08/2008 for the course EE 361 taught by Professor Conry during the Spring '08 term at Clarkson University .

Page1 / 18

L13 - strings - Strings, Strings, Strings We've seen...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online