© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students C-strings and strings
1 Simple and Composite Variables We have studied simple variables A simple variable describes a single value A simple variable has an identifier A simple variable has a type that describes the properties of the value of the variable, the permissible operations for the variable, and the representation of the variable in computer memory We can also have composite variables These variables describe a group of values Arrays: all values in the group have the same type Structures: different values in the group can have different types
2 Composite Variables composite variables describe a group of values 1 dimensional arrays or variables of a particular type (all entries must have the same type) multi dimensional arrays or variables of a particular type (all entries must have the same type) Structures containing groups of variables of different types Strings are another special type that builds on arrays An array of characters A set of special operations appropriate for text
3 One-Dimensional (1-D) Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element of an array is accessed using the array name and an index or subscript The name of the array is the address of the first element and the subscript is the offset In C and C++, the subscripts always start with 0 and increment by 1
Strings (C++ string objects) Easy to manipulate using operators like =, ==, =,, + Only available in C++, there are no objects in C Cannot mix C-strings and strings without doing explicit conversion/copy Often system functions use C-strings so changing your strings to C-strings may be necessary 4
C-strings In C if we wish to use a group of characters as a variable we can create a C-string. The C-string is an extension of an array of characters. The C-string can be manipulated using functions from the library for pure C for using C-strings in C++ 5
C-strings: limitations C-strings cannot be combined using the operators we are used to like =, ==, + Instead of using operators we use functions from the C-string library When using C strings we must take extra care to check that we are not accessing memory past the end of the allocated character array that holds the C-string 6
7 1-D Character array char list[10]; allocates memory for 10 characters. Ten adjacent locations in memory are allocated Remember C++ does not perform any bounds checking on arrays list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[9] list[8]
8 Initializing 1-D Arrays Strings are not the same as 1-D character arrays You can specify individual values for each character in a 1-D character array /* put one character in each element of the array*/ char list[5] = {‘h’,’e’,’l’,’l’, ‘o’}; After initialization memory looks like list[0] ‘h’ list[1] ‘e’ list[2] ‘l’ list[3] ‘l’ list[4] ‘o’ Because there is no null termination character the array cannot be used as a C-string
9 Difference: C-string vs 1D character array C-Strings are character arrays with special properties that are operated on using functions from the string library C-strings are partially filled character arrays that are used in a particular way in C++ or in C A C-string always ends with a null termination character (\0) The null termination character tells all the functions in the string library where the string ends, allowing C-strings to be manipulated by the C string library
10 Strings of different lengths Strings of different lengths can be stored in a character array The maximum number of characters in the string is the number of characters in the array minus one Blanks can be included in the string Blanks count as characters char list[9] = {“hello”}; char list1[9] = {“hi jane”}; list[0] ‘h’ list[1] ‘i’ list[2] ‘ ’ list[3] ‘j’ list[4] ‘a’ list[5] ‘n’ list[6] ‘e’ list[7] ‘\0’ list[8] ‘\0’ list[0] ‘h’ list[1] ‘e’ list[2] ‘l’ list[3] ‘l’ list[4] ‘o’ list[5] ‘\0’ list[6] ‘\0’ list[7] ‘\0’ list[8] ‘\0’
11 Avoid a common problem (1) C and C++ do not perform any bounds checking on arrays This means that you can accidentally change the values of other variables. Changing the value of an array element that is not actually part of your array may change the value of one of your other variables For a C-string variable this is particularly easy. You must remember that character array mystring[20] holds a string of no more than 19 characters “hello my friend” has 15+1 characters “joe” has 3+1 characters REMEMBER THE NULL TERMINATION CHARACTER
12 Avoid a common problem (2) int count = 3; char myArray[5] = {“hello”}; After the first declaration memory looks like After the second declaration statement above REMEMBER: Leave room for the \0 myArray[0] ‘h’ myArray[1] ‘e’ myArray[2] ‘l’ myArray[3] ‘l’ myArray[4] ‘o’ count ‘\0” myArray[0] ? myArray[1] ? myArray[2] ? myArray[3] ? myArray[4] ? count 3
13 Avoid a common problem (3) C++ does not perform any bounds checking on arrays By initializing or changing the contents of a string using functions from, a string that is longer than will fit into the character array associated with the string can be placed in the string, This may change the value of a completely different variable It is imperative that you be very careful to avoid using strings longer than the allocated space
14 Avoid a common problem (3) int count [4]= {1,2,3,5}; char mySt[5] ; After the first declarations memory looks like After putting the string “my name” into the variable mySt mySt has no terminating \0, string library breaks In addition the array count has been corrupted mySt[0] ? mySt[1] ? mySt[2] ? mySt[3] ?? Count[0] 1 Count[1] 2 Count[2] 3 Count[3] 5 mySt[0] ‘m’ mySt[1] ‘y’ mySt[2] ‘ mySt[3] ‘n’ mySt[4] ‘a’ Count[0] ‘m’ Count[1] ‘e’ Count[2] ‘\0’ Count[3] 5 mySt[4]
15 Arrays of strings or C-strings Declare your array of Strings #define NUMNAMES 20 #define MAXNAMELEN 32 char names[NUMNAMES][MAXNAMELEN] Declare and Initialize your array #define NUMMONTHS 12 #define MONTHNAMESIZE 10 char month[NUMMONTHS][MONTHNAMESIZE] = { “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, December” };
16 Initializing and array of strings or C-strings char month[12][10] = { “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, December” }; ‘J’ ‘a’‘n’‘u’‘a’‘r’ ‘y’ ‘\0’ ‘F’ ‘e’‘b’‘r’‘u’‘a’ ‘r’ ‘y’‘\0’ ‘M’ ‘a’‘r’‘c’‘h’‘\0’ ‘A’ ‘p’‘r’‘i’‘l’‘\0’ ‘M’ ‘a’‘y’‘\0’ ‘J’ ‘u’‘n’‘e’‘\0’ ‘J’ ‘u’‘l’‘y’‘\0’ ‘A’ ‘u’‘g’‘u’‘s’‘t’ ‘0’ ‘\0’
17 Initializing and array of strings or C-Strings char month[12][10]; Month[0] = “January”: Month[10] = “November”; Month[11][1] = ‘D’; Month[11][7] = ‘r’; ‘J’ ‘a’‘n’‘u’‘a’‘r’ ‘y’ ‘\0’ ‘N’ ‘o’‘v’‘e’‘m’‘b’ ‘e’ ‘r’‘\0’ ‘D’ ‘e’‘c’‘e’‘m’‘b’ ‘e’ ‘r’?? ‘\0’
18 Initializing or changing a string or C-string You can initialize or change the value of a C++ string variable by using the statement myString = “this is my string”; Anywhere in your code You can initialize the values of a C-string in a declaration by using a statement like char myString[20] = “this is my string”; After declaration you can only change the value of a C-string using a function from the string.h like strcpy(myString, “this is my string”);
19 Putting data into a 1-D Array Another common way of assigning values to C- strings or arrays of C-strings is to read data values from a file directly into the string or array of strings Each value read from the file is assigned to a single string(for example names[6]) A single row stored in the ith row in the array of strings names is referred to as names[i], Note that checks to determine the file was opened correctly and that data was read correctly have been omitted from the example, they should not be omitted from your code
20 Array of C-strings read from a data file #define NUMPEOPLE 30 #define NAMELEN 32 char names[NUMPEOPLE][NAMELEN]; char title[30]; int ages[NUMPEOPLE]; int k; ifstream inStream; inStream.open(“registrants”); cin >> title; cout << title << endl; for(k=0; k<NUMPEOPLE; k++) { inStream >> names[k] >> ages[k]; cout << names[k] << “ “ << ages[k] << endl; }
21 Notes on array input When you read or write a string or C-string your read or write all characters in that string The final character in the string is determined by the location of the null termination character \0
22 Strings as function parameters Arrays, or parts of arrays, can be passed as arguments to functions. An element of a C-string can be used as a simple character variable parameter It can be passed by value or by reference An entire array containing a C-string can be used as a parameter of a function It can only be passed by reference using the name of the string (the name of the string is a reference to the location in memory of the first character in the string)
23 Strings as a data type Remember a data type has a group of objects (things) that can be combined in different ways using the operands for that data type. The operands for C-strings are not those used for other data types (like +, -, = …) All operations on C-strings are performed using functions from the string library (other than reading and writing) To include the C-string library in your program include C++ include C
24 Assigning C-strings You have seen that = can be used when assigning values to strings in a declaration = cannot be used to assign a string literal to a C-string: The following is not valid mystring = “testinput”; To copy one string to another the string library functions strcpy is usually used strcpy(mystring, “testinput”); strcpy(myCopiedString, myOriginalString);
25 Assigning strings To copy one string to another the string library functions strcpy is usually used To copy part of a string, (a substring) or to assure you do not copy more characters into a string that it can hold you can also use strncpy strncpy(mystring, “testinput”, 5); Note that strncpy copies the first 5 characters of “testinput” only (testi) and does not add a \0 to the end of the copied string strcpy copies the entire string (even it it is longer than the available space!) including the \0
26 Avoid a common problem (3) int count [4]= {1,2,3,5}; char mySt[5] ; strcpy(mySt, “my name”); After the declarations memory looks like After the strcpy statement above mySt has no terminating \0 in its array mySt[0] ? mySt[1] ? mySt[2] ? mySt[3] ? mySt[4] ? Count[0] 1 Count[1] 2 Count[2] 3 Count[3] 5 mySt[0] ‘m’ mySt[1] ‘y’ mySt[2] ‘ mySt[3] ‘n’ mySt[4] ‘a’ Count[0] ‘m’ Count[1] ‘e’ Count[2] ‘\0’ Count[3] 5
27 Finding the length of a string To find the number of characters actually stored in a string use the string library function strlen int len; char mystring[30]; strcpy(mystring, “testing”); len = strlen(mystring); /* len now has a value 7 */ strlen counts the number of characters in the string not including the terminating \0
28 Concatenating Strings Combining two strings into a single string Use the string library functions strcat or strncat strcat and strncat take one string and append it to the end of another string The terminating \0 is removed from the end of the first string before the second string is added The terminating \0 is replaced at the end of the second string strcat and strncat can create a string too long to fit in the allocated string storage:
29 Example: using strcat char name1[10] = “marie”; char name2[10] = “anne”; strcat( name2, name1); marei\0‘\0’ ann\0e‘\0’ marei\0‘\0’ annmeari\0e
30 Concatenating Strings char mystring[20]=“start input: “; char mystring1[20] = “input1”; char mystring2[20] = “ and output” /* after the following strcat mystring1 contains */ /* “input1 and output” */ strcat(mystring1, mystring2); /* after the following strcat mystring1 contains */ /* “start input: input1 and output” */ /* this string overflows the array mystring */ strcat(mystring, mystring1);
31 Example: using strcat char mystring[20]=“start input: “; char mystring1[20] = “input1 and output”; strncat( mystring, mystring1); The array is overrun statr inup inptu1adn t: \0 ouptut statr inup inptu1adn t: ni p ut1 ouptut
32 Concatenating Strings #define STRLEN 20 int len, added; char mystring[STRLEN]=“start input: “; char mystring1[STRLEN] = “input1 and output”; /* To prevent overflow find the number of */ /* characters that can be added to mystring */ /* added (6) = STRLEN (20) – len(13) – 1 */ len = strlen(mystring); added = STRLEN – len -1; /* after the following strcat mystring1 contains */ /* “start input: input1” */ strncat(mystring, mystring1, added);
33 Comparing Strings To compare 2 strings usually use the string library function strcmp strcmp(mystring1, mystring2) strcmp returns an integer, if mystring1 is alphabetically before mystring2 a negative number will be returned If the strings are identical 0 will be returned if mystring2 is alphabetically before mystring1 a positive number will be returned
34 ASCII equivalents Each alphabetic character, number, or other character (including whitespace characters) has an integer equivalent value These integer values are used by strcmp to determine the alphabetical ordering. All uppercase letters precede lower case letters All numbers precede uppercase letter A string st1 contains the first few characters of a longer string st2. st1 precedes st2 when compared
Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex (nul) x00 | (sp) x x40 | ` x60 (soh) x01 | ! x21 | A x41 | a x61 (stx) x02 | " x22 | B x42 | b x62 (etx) x03 | # x23 | C x43 | c x63 (eot) x04 | $ x24 | D x44 | d x64 (enq) x05 | % x25 | E x45 | e x65 (ack) x06 | & x26 | F x46 | f x66 (bel) x07 | ' x27 | G x47 | g x67 (bs) x08 | ( x28 | H x48 | h x68 (ht) x09 | ) x29 | I x49 | i x69 (nl) x0a | * x2a | J x4a | j x6a (vt) x0b | x2b | K x4b | k x6b (np) x0c |, x2c | L x4c | l x6c (cr) x0d | x2d | M x4d | m x6d (so) x0e | x2e | N x4e | n x6e (si) x0f | / x2f | O x4f | o x6f (dle) x10 | x30 | P x50 | p x70 (dc1) x11 | x31 | Q x51 | q x71 (dc2) x12 | x32 | R x52 | r x72 (dc3) x13 | x33 | S x53 | s x73 (dc4) x14 | x34 | T x54 | t x74 (nak) x15 | x35 | U x55 | u x75 (syn) x16 | x36 | V x56 | v x76 (etb) x17 | x37 | W x57 | w x77 (can) x18 | x38 | X x58 | x x78 (em) x19 | x39 | Y x59 | y x79 (sub) x1a | : x3a | Z x5a | z x7a (esc) x1b | ; x3b | [ x5b | { x7b (fs) x1c | x3e | ^ x5e | ~ x7e (us) x1f | ? x3f | _ x5f | (del) x7f 35
36
37 Comparing Parts of Strings To compare the first n characters of 2 strings use the string library function strncmp strncmp(mystring1, mystring2, n) strncmp returns an integer, if mystring1 is alphabetically before mystring2 a negative number will be returned If the strings are identical 0 will be returned if mystring2 is alphabetically before mystring1 a positive number will be returned
38 Character analysis You can also analyze a string (or a character array) one character at a time The ctype library #include includes functions for such analysis. Each of these functions returns an integer value. The value is nonzero if the condition checked is true (0 if it is false) isalpha(char mychar); /* is an alphanumeric char */ isdigit( char mychar); /* is a numeral */ ispunct(char mychar); /* is a non whitespace punctuation character */ isspace(char mychar); /* is a whitespace character */ tolower(char mychar); /* converts alphanumeric to lower case */ toupper(char mychar); /* converts alphanumeric to upper case */h
39 The ctype and cstring Libraries We have had an introduction to some of the functions in these libraries. These libraries are much more flexible than this subset of functions indicates You should be able to read the function descriptions for the other functions in the string library and then use those functions in your programs
40 C-String Output Can output with insertion operator, << As we’ve been doing already: cout << news << " Wow.\n"; Where news is a c-string variable Possible because << operator is available for C-strings!
41 C-String Input Can input with extraction operator, >> Issues exist, however Whitespace is "delimiter" Tab, space, line breaks are "skipped" Input reading "stops" at delimiter Watch size of c-string Must be large enough to hold entered string! C++ gives no warnings of such issues!
42 C-String Line Input Can receive entire line into c-string Use getline(), a predefined member function: char a[80]; cout << "Enter input: "; cin.getline(a, 80); cout << a << "END OF OUTPUT\n"; Dialogue: Enter input: Hello friend! Hello friend!END OF INPUT
43 More getline() Can explicitly tell length to receive: char shortString[5]; cout << "Enter input: "; cin.getline(shortString, 5); cout << shortString << "END OF OUTPUT\n"; Results: Enter input: Goodbye friend; GoodEND OF OUTPUT Forces FOUR characters only be read Recall need for null character!
44 Character I/O Input and output data ALL treated as character data e.g., number 10 outputted as '1' and '0' Conversion done automatically Uses low-level utilities Can use same low-level utilities ourselves as well
45 Member Function get() Reads one char at a time Member function of cin object: char nextSymbol; cin.get(nextSymbol); Reads next char & puts in variable nextSymbol Argument must be char type Not "string"!
46 Member Function put() Outputs one character at a time Member function of cout object: Examples: cout.put('a'); Outputs letter "a" to screen char myString[10] = "Hello"; cout.put(myString[1]); Outputs letter "e" to screen
47 More Member Functions putback() Once read, might need to "put back" cin.putback(lastChar); peek() Returns next char, but leaves it there peekChar = cin.peek(); ignore() Skip input, up to designated character cin.ignore(1000, '\n'); Skips at most 1000 characters until '\n'
48 Standard Class string Defined in library: #include using namespace std; String variables and expressions Treated much like simple types Can assign, compare, add: string s1, s2, s3; s3 = s1 + s2;//Concatenation s3 = "Hello Mom!"//Assignment Note c-string "Hello Mom!" automatically converted to string type!
49 I/O with Class string Just like other types! string s1, s2; cin >> s1; cin >> s2; Results: User types in: May the hair on your toes grow long and curly! Extraction still ignores whitespace: s1 receives value "May" s2 receives value "the"
50 getline() with Class string For complete lines: string line; cout << "Enter a line of input: "; getline(cin, line); cout << line << "END OF OUTPUT"; Dialogue produced: Enter a line of input: Long and curly? Long and curly?END OF INPUT Similar to c-string’s usage of getline()
51 Pitfall: Mixing Input Methods Be careful mixing cin >> var and getline int n; string line; cin >> n; getline(cin, line); If input is:42 Hello hitchhiker. Variable n set to 42 line set to empty string! cin >> n skipped leading whitespace, leaving "\n" on stream for getline()!
52 Class string Processing Same operations available as c-strings And more! Over 100 members of standard string class Some member functions: .length() Returns length of string variable .at(i) Returns reference to char at position i
53 C-string and string Object Conversions Automatic type conversions From c-string to string object: char aCString[] = "My C-string"; string stringVar; stringVar = aCstring; Perfectly legal and appropriate! aCString = stringVar; ILLEGAL! Cannot auto-convert to c-string Must use explicit conversion: strcpy(aCString, stringVar.c_str());