Strings A collection of characters taken as a set: A word, a phrase, a name, an address, a phone number, a Social Security Number In a program, Strings are stored as a unit in a char array. A special character is added to the end of the characters to mark the end of the String, an ASCII 0 character. The arrays need to be declared one element larger than the largest String you expect. ASCII 0 Character - the char whose ASCII value is 0. SHOW THEM THE LAYOUT OF A STRING IN AN ARRAY.
cout << "Enter your first name: "; cin >> firstName; char firstName[16], lastName[26]; cout << "Enter your first name: "; cin >> firstName; cout << "Enter your last name: "; cin >> lastName; cout << "Your name is " << firstName << " " << lastName << ".\n"; Enter your first name: Lee Enter your last name: Weiner Your name is Lee Weiner. Press any key to continue Notice I just use the name of the arrays in the cin and cout statements. No empty brackets. NEXT to show output.
Assigning Strings to Array Variables Initializing the array when declared char firstName[16] = "Lee", lastName[26] = "Weiner"; Using cin for user input cin >> firstName; In an assignment statement firstName = "Susan"; EACH BULLET APPEARS SEPARATELY
Some Other Things I Can't Do With Strings cout << firstName + LastName; if( firstName == lastName ) if( firstName < secondName ) firstName = secondName;
String Functions strlen( <string> ) - Returns an int that is the number of characters in the string. strcpy( <target>, <source> ) - Copies the contents of the source string to the target variable. Also used in place of an assignment statement. strcmp( <str1>, <str2> ) - Compares two strings. Returns an int indicating the relationship between the two. Need to #include <string.h>