120 likes | 231 Views
An Array Type For Strings. Two ways to represent strings – i.e. “Hello”. cstring. string. New way to process string types Discussed next section. An array with base type char Older way of processing strings Null character marks end of string. A closer look at cstring.
E N D
Two ways to represent strings – i.e. “Hello” cstring string New way to process string types Discussed next section • An array with base type char • Older way of processing strings • Null character marks end of string
A closer look at cstring • An array of characters • “Hello” stored as six indexed variables, ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, and the null character • Null character used as an end marker, distinct from all ‘real’ characters • ‘\0’ • Is a single character, just like tab or newline • How many characters is a c-string variable capable of holding if it is declared as: • char s[10];
Partially filled c-string variables • Does not use an int variable to keep track of the number of slots filled • Uses the null character to mark the last character in the c-string • Draw: “Hi Mom!” if in the array char s[10]; char s[10] = “Hi Mom!”; • General declaration: char my_c_string[maxsize + 1] why +1?
Initializations • Example 1: char myMessage[20] = “Hi there.”; • Example 2: char shortString[] = “abc”; • Example 3: char shortString[4] = “abc”; • Example 4: char shortString[] = {‘a’, ‘b’, ‘c’}; • Are examples 2 and 4 equivalent? Why/why not? • Initializing like we did in examples 1, 2, and 3 automatically places the null character in the array at the end of the specified characters • If the size is omitted (like in example 2), then the c-string variable will be given a size one character longer than the length of the c-string
Consider the declaration: • char ourString[5] = “Hi”; • Indexed Variables: • ourString[0] • ourString[1] • ourString[2] • ourString[3] • ourString[4] • Write the code that will change ourString to a c-string of the same length consisting of all ‘X’ characters • You should be careful not to replace the null character • Many string manipulating functions depend on the presence of the null character
Using = and == with C strings • You can’t use the assignment operator with c-strings as you would with normal variables • Assignment is different from initialization (which is legal) • Ex: char aString[10]; aString = “Hello”; //illegal! • The easiest way to assign a value to a c-string variable is to use the strcpy function • Ex: strcpy(aString, “Hello”); • Be careful – this function does not check that the value assigned is within the appropriate length • A safer version: strncpy • Takes a third argument that gibes the max number of characters to copy • Ex: char anotherString[10]; strncopy(anotherString, aStringVariable, 9); • Instead of using == to test equality of c-string variables, use the function strcmp • Ex: if(strcmp(cString1, cString2)) cout << “strings are not the same” << endl; else cout<< “Strings are the same” << endl;
The <cstring> library • Not needed to declare/initialize c-string variables • Needed for many predefined functions needed to process c-string variables
C-String Arguments and Parameters • Keep in mind that a c-string is an array, so it behaves as such as a function parameter • Safest to include an int parameter giving the declared size of the c-string variable
Input and Output • Can use << for output • Can use >> for input, but keep in mind how it behaves with white space • Ex 1 in code • Can also use the getline() function for input • Accepts two arguments, the first the c-string variable, the second the size of the c-string • Ex 2 in code
C-String-to-Number conversions • All functions accept a c-string argument • See ex. 3 in code