Download presentation
Presentation is loading. Please wait.
1
CPS120: Introduction to Computer Science
Strings (Part I)
2
String Basics A string is a sequence of characters that may or may not spell a word When one needs to refer to a variable that contains multiple characters, he or she needs to use a string variable
3
Character Literals A character literal is a single character that is used in a C++ program between single quotes. In the statement, initial = 'T'; the character literal is 'T'
4
String Literals A string literal is a sequence of characters that is used in a C++ program between double quotes such as in the statement cout << "Hello world!"; where "Hello world!" is the string literal Note: that the string literal, in this case, includes a space and an exclamation point Similar to a constant Ends with an invisible null terminator (ASCII 0) Represented as \0
5
Character Arrays Used to store strings that change as the program runs
Unlike string literal An array is a group of variables of the same data type that appear together in memory In this case each variable holds a character and the last variable in the string holds the null terminator (/0)
6
Declaring Character Arrays
You must always save room for the null terminator when declaring a character array for strings char stu_name[21] = “Oliver Williamson”; The null character is automatically entered when double quotes are used Could also declare as; char stu_name[] ={‘O’,’l’,’i’,’v’,’e’,’r’,’ ‘,’W’,’i’,’l’,’l’,’i’,’a’,’m’,’s’,’\0’}; A character array and a string are different. In C++ you generally declare a character array rather than a string. A string may then be stored in a character array
7
Fixed Data Declarations
If all values stored in the array will have the same format (i.e. size), you can declare without a number in the brackets char phone_no[] = “(734) ”; Do not leave the brackets empty when declaring a character array if you have no initialization string The compiler creates an array just large enough for your string and a null terminator Open up StringIO.cpp
8
Getting Into Trouble C++ does not prevent you from storing a string that is too long in a character array Char state[3] = “TX”; … strcopy (state, “California”); The value spills over into other locations and overwrites them Does unpredictable things Might crash the computer Open up sterror.cpp
9
Declaring an Array We can only “assign a multiple character constant to an array at the moment of initialization The lvalue of an assignation can only be an element of the array and not the entire array Could do: myString[0] =‘Y’; myString[1] = ‘e’; myString[2]=‘s’;
10
Using String Functions
Instead of the prior method, use: strcopy (myString, “Yes”); Need to #include<string.h> to use strcopy
11
Using Strings In Programs
In order to use string variables within any C++program, you must use the compiler directive: #include <string.h> At the top of your program. You must also refer to iostream.h header file as iostream #include <iostream.h> you must add the statement below your last #include directive : using namespace std; The following program uses a string variable and properly includes the necessary header files: #include <iostream> #include <string> using namespace std; int main() { string name = "John Doe"; cout << name; return 0; }
12
Declaring String Variables
You must declare a string variable in your program before you wish to use the variable. Example: string studentName; string studentName = "John Doe"; declares AND initializes the variable, studentName, in the same statement.
13
Assigning Strings You can use the assignment operator to assign the value of one string to another string as in: studentName = personName;
14
String Functions You can determine the current length of a string with the use of a string's length function. For example: string name = “Paul"; int lengthOfName = 0; lengthOfName = name.length( ); cout << "His name is " << lengthOfName << " letters long.";
15
cstring Functions strcat strcmp
Char* strcat (char[] dest, const char[] src); Appends src string at end of dest. Returns dest. strcmp int strcmp (const char[] string1, const char[] string2); Compares string1 and string2 and returns 0 if both are equal Char* is the same as []
16
More cstring Functions
strcpy char [] strcpy (char[] dest, const char[] src); Copies the content of src to dest and returns dest strlen Size_t strlen (const char[] string); Returns the length of string Char* is the same as []
17
Converting Strings to Other Types
atoi: converts string to int type atol: converts string to long type atof: converts string to float type All of these functions admit one parameter and return a value of the requested type cin.getline (myInput, 100); quantity = atoi (myInput);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.