

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Character Arrays (Strings) Definition: A string is an array of ________________________________ Declaration syntax: char stringName[numberOfChars]; The “numberOfCharacter” must be large enough to hold ALL characters in the string AND an extra character: the _______________________ which marks the end of the string. EX: char greeting[6] = "hello"; Using strings in a program/special string operations: Input/Output: ARE defined as aggregate operations char firstName[20]; cin >> firstName; cout << “The name that was entered is: “ << firstName << endl; Alternate methods of assigning a value to a string: Assignment of a literal string value: char example[6]; //must be exactly the right size example = “hello”; Copying a string: Cannot use assignment operator with non-literal strings (=) Instead there is a special string library implemented by C++ called cstring To copy a string, use the strcpy( ) function Function heading: void strcpy(char toString[ ], char fromString[ ]) #include
Comparing strings: Can not use the traditional = =, <, >, operators with cstrings Use the strcmp( ) function in the cstring library Function heading: int strcmp(char firstString[ ], char secondString[ ]) Return value means: //if return value is ______________, str1 precedes str2 lexicographically //if return value is ______________, str1 follows str2 lexicographically //if return value is ______________, if the strings are identical #include