Docsity

C++ Character Strings: Declaration, Assignment, Comparison, and Length, Study notes of Computer Science

Toggle sidebar
bg1
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 <cstring>
…
char name1[50]
char name2[50];
cin >> name1 >> name2;
cout << name1 << " " << name2 << endl;
//Statement to copy name1 into name2
cout << name1 << " " << name2 << endl;
…
pf2

Partial preview of the text

Download C++ Character Strings: Declaration, Assignment, Comparison, and Length and more Study notes Computer Science in PDF only on Docsity!

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 … char name1[50] char name2[50]; cin >> name1 >> name2; cout << name1 << " " << name2 << endl; //Statement to copy name1 into name cout << name1 << " " << name2 << endl; …

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 #include int main( ) { char name1[50]; char name2[50]; cin >> name1 >> name2; // example using strcmp same = strcmp(name1, name2); if (same == 0) cout << "The names " << name1 << " and " << name << " are the same." << endl; else if (same < 0) cout << name1 << " comes before " << name << " in the alphabet." << endl; else if (same > 0) cout << name1 << " comes after " << name << " in the alphabet." << endl; … } Finding the number of characters stored in a string: Use the function strlen( ) in the cstring library int strlen(char str[]); Returns length of the string ___________________________________________ #include int main( ) { char name1[50]; cin >> name1; ????? //Function call to determine length of name cout << "The string " << name1 << " contains " << length << " characters." << endl; }