Professional Documents
Culture Documents
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
1
Objectives
To learn how to declare a enumerated type and assign a value to it (§7.1).
To test and convert characters using the character functions (§7.9.1).
To store and access strings using arrays and pointers (§7.9.2).
To read strings from the keyboard (§7.9.3).
To process strings using the string functions (§7.9.4).
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
2
Enumerated Types
enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY};
Day day;
The variable day can hold one of the values defined in the
enumerated type. For example, the following statement assigns
enumerated value MONDAY to variable day:
day = MONDAY;
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
3
Enumerated Types
As with any other type, you can declare and initialize a variable in
one statement:
TestEnumeratedType Run
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
4
#include <iostream>
using namespace std;
int main()
{
enum Day {MONDAY = 1, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY} day;
cout << "Enter a day (1 for Monday, 2 for Tuesday, etc): ";
int dayNumber;
cin >> dayNumber;
switch (dayNumber) {
case MONDAY:
cout << "Play soccer" << endl;
break;
case TUESDAY:
cout << "Piano lesson" << endl;
break;
case WEDNESDAY:
cout << "Math team" << endl;
break;
default:
cout << "Go home" << endl;
}
return 0;
}
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
5
Characters and C-Strings
Strings are used often in programming. You have
used string literals. A string is a sequence of
characters. There are two ways to process strings
in C++. One way is to treat strings as arrays of
characters. This is known as pointer-based strings
or C-strings. The other way is to process strings
using the string class. The string class will be
introduced in §9.14. This section introduces
pointer-based strings.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
6
Character Test Functions
Function Description Example
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
7
Case Conversion Functions
CharacterFunctions Run
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
8
Storing and Accessing Strings
A pointer-based string in C++ is an array of characters
ending in the null terminator ('\0'), which indicates where a
string terminates in memory. An array can be accessed via a
pointer. So a string can also be accessed via a pointer,
which points to the first character in the string. So you can
declare a string variable using an array or a pointer. For
example, the following two declarations are both fine:
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
9
Pointer Syntax
You can access city or pCity using the array syntax or
pointer syntax. For example,
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
10
Reading Strings
You can read a string from the keyboard using the
cin object. For example, see the following code:
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
11
Reading Strings Using getline
C++ provides the cin.getline function in the iostream
header file, which reads a string into an array. The syntax
of the function is:
String Returns the length of the string, i.e., the number of the
characters before the null terminator.
char *strcpy(char *s1, const char *s2)
Functions
Copies the string s2 to string s1. The value in s1 is returned.
char *strncpy(char *s1, const char *s2, size_t n)
Copies at most n characters from string s2 to string s1. The
value in s1 is returned.
char *strcat(char *s1, const char *s2)
Appends string s2 to s1. The first character of s2 overwrites the
null terminator in s1. The value in s1 is returned.
char *strncat(char *s1, const char *s2, size_t n)
Appends at most n characters from string s2 to s1. The first
character of s2 overwrites the null terminator in s1 and appends
a null terminator to the result. The value in s1 is returned.
int *strcmp(char *s1, const char *s2)
Returns a value greater than 0, 0, or less than 0 if s1 is greater
than, equal to, or less than s2 based on the numeric code of the
characters.
int *strncmp(char *s1, const char *s2, size_t n)
Returns a value greater than 0, 0, or less than 0 if the n
characters in s1 is greater than, equal to, or less than the first
n characters in s2 based on the numeric code of the characters.
int atoi(char *s1)
Converts the string to an int value.
double atof(char *s1)
Converts the string to a double value.
long atol(char *s1)
Converts the string to a long value.
void itoa(int value, char *s1, int radix)
Converts the value to a string based on specified radix.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
13
String Examples
CopyString Run
CombineString Run
CompareString Run
StringConversion Run
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
14
Copy String
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[20];
char s2[20] = "Dallas, Texas";
char s3[20] = "AAAAAAAAAA";
strcpy(s1, s2);
strncpy(s3, s2, 6);
s3[6] = '\0'; // Insert null terminator
return 0;
}
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
15
Combine String
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[20] = "Dallas";
char s2[20] = "Texas, USA";
char s3[20] = "Dallas";
return 0;
}
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
16
String Conversion
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout << atoi("4") + atoi("5") << endl;
cout << atof("4.5") + atof("5.5") << endl;
char s[10];
itoa(42, s, 8);
cout << s << endl;
itoa(42, s, 10);
cout << s << endl;
itoa(42, s, 16);
cout << s << endl;
return 0;
}
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
17
Obtaining Substrings
Often it is useful to obtain a substring from a string.
But there is no such function in C++. You can
develop your own function for extracting substrings.
The header of the function can be specified as:
char * substring(char *s, int start, int end)
Substring.h
TestSubstring Run
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
18
Checking Palindromes
A string is a palindrome if it reads the same
forward and backward. The words “mom,” “dad,”
and “noon,” for example, are all palindromes.
CheckPalindrome Run
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
19