0% found this document useful (0 votes)
4 views

lec13-string

Uploaded by

Mamat Rahmat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

lec13-string

Uploaded by

Mamat Rahmat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

CSCI1540

Fundamental Computing
with C++
Characters and Strings

Fall, 2024
Characters
• A character (a char-type value) is represented
internally as an 8-bit integer that corresponds to
the ASCII code of the character
• ASCII: American Standard Code for Information
Interchange

• E.g., 'A' is 65, '1' is 49, 'a' is 97, ' ' is 32, …
• See next page for the ASCII code of each character

• Note: Non-English characters are represented using


other data types. (Won't be discussed in this
course)
2
ASCII Table
0 1 2 3 4 5 6 7 8 9
0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT
10 NL VT NP CR SO SI DLE DC1 DC2 DC3
20 DC4 NAK SYN ETB CAN EM SUB ESC FS GS
30 RS US SP ! " # $ % & '
40 ( ) * + , - . / 0 1
50 2 3 4 5 6 7 8 9 : ;
60 < = > ? @ A B C D E
70 F G H I J K L M N O
80 P Q R S T U V W X Y
90 Z [ \ ] ^ _ ` a b c
100 d e f g h i j k l m
110 n o p q r s t u v w
120 x y z { | } ~ DEL
3
Some Special Characters
Symbol ASCII Description Escaped character in C++
NUL 0 Null character '\0'
BEL 7 Bell (cause a beep) '\a'
BS 8 Backspace '\b'
HT 9 Horizontal tab '\t'
NL 10 Line feed '\n'
VT 11 Vertical tab '\v'
NP 12 Formfeed '\f'
CR 13 Carriage return '\r'
ESC 27 Escape
SP 32 Space ' '
DEL 127 Delete
4
char-Type Values
• A char-type value, when used in an arithmetic
expression, is treated as an integer
• In output, a char-type value appears as a character
• Examples:
1 cout << 'A'; // Prints A
2
3 int x = 'A'; // 'A' is coerced to 65 first
4 char ch = 65; // ch is assigned symbol of ASCII 65
5
6 cout << x; // Prints 65 because x is of type int
7 cout << ch; // Prints A because ch is of type char
8
9 ch = ch + 1; // ch becomes symbol of ASCII 66
10 cout << ch; // Prints B 5
Manipulating Characters: Example
1 char ch;
2 …
3 /* If ch holds a lowercase letter, output the letter's
4 "position" in the English alphabets.
5 'a' is the 1st letter, 'z' is the 26th letter */
6 if (ch >= 'a' && ch <= 'z')
ch >= 'a' and ch >= 97 are evaluated
7 cout << ch - 'a' + 1;
equally but the former is more meaningful
8
9 /* If ch holds an uppercase letter, convert the letter to
10 the equivalent lowercase letter */
11 if (ch >= 'A' && ch <= 'Z')
12 ch = 'a' + (ch - 'A');
13
14 /* If ch holds a digit ('0' to '9'), calculate the integer
15 value represented by that digit */
16 if (ch >= '0' && ch <= '9') {
17 int digitValue = ch - '0';
18 …
19 } 6
(Optional)

Character Manipulation Functions


Need to #include <cctype>
Functions Returns a non-zero value if ch is one of
these characters:
int isalpha(int ch); 'A' – 'Z', 'a' – 'z'
int isdigit(int ch); '0' – '9'
int islower(int ch); 'a' – 'z'
int isupper(int ch); 'A' – 'Z'
int isspace(int ch); ' ', '\n', '\t', '\f', '\v', '\r'
Functions Returns:
int tolower(int ch); The equivalent lowercase letter if ch is
uppercase; otherwise returns ch
int toupper(int ch); The equivalent uppercase letter if ch is
lowercase; otherwise returns ch
Reference: https://cplusplus.com/reference/cctype/ 7
Strings
• A string is a sequence of characters
• E.g.: "I love CSCI!"

• String length = the total number of characters in


the string
• E.g., Length of "ABC DEF\n" is 8
• \n is considered as one character only

• Each character in a string has a position


• The 1st character has position 0
• The last character has position string_length - 1
8
String "…" vs Character '…'
• A string is enclosed by a pair of double quote
• "A" – A string containing only one character A
• "ABC" – A string containing three characters
• "" – An empty string (A string that contains no character)

• A character is enclosed by a pair of single quote


• 'A' – A character A
• '\n' – A newline character
• '\0' – A null character
• '' – Error! We don’t have “empty character”

• The data type of a string in the form "…" is array of


char (i.e., “char []”, or “char *”)
9
Representing Strings in C++
• There are two representation of strings in C++

1. As null-terminated strings
• A null-terminated string (also called c-string) is an array
of char (i.e., “char []”, or “char *”)
• The end of the string is indicated by a null character
('\0', ASCII value 0) in the array

2. As string objects

10
Null-terminated Strings (c-strings)
• Inherited from the C Language

• A null-terminated string is represented as an array


of characters in which the first null character (i.e.,
the one with the smallest index) in the array
indicates the end of the string
Manually insert
the null character
• E.g.:
char s1[7] = { 'A', 'B', 'C', '\0', 'D', '\0', 'E' };
when treated as a string, represents “ABC”
A B C \0 D \0 E
Index 0 1 2 3 4 5 6 11
(Optional)

Initializing an Array of char as a


C-String • Initialize the first 4 positions as 'A',
'B', 'C', and '\0'
• s1 can store any strings with length ≤ 9
1 char s1[10] = "ABC";
2 Null character is
3 // s2 has size 11. Last character is '\0' automatically
4 char s2[] = "abcdefghij"; added
5
6 cout << s1 << endl; // Output ABC
7 cout << s2 << endl; // Output abcdefghij
8
9 s2[4] = '\0'; // Replace 'e' by '\0'
10 cout << s2 << endl; // Output abcd
11 // Note: s2[5...10] remains unchanged
12
13 cout << s2[6] << endl; // Output g

s2 a b c d \0
e f g h i j \0
12
(Optional)

Processing C-Strings
• You can process a c-string as if it is an array

• A few things to pay attention to:


• Need a null character in the array to indicate the end of
a string
• An array of size 𝑁 can store any string containing up to
𝑁 − 1 characters. (Need one more space for the null
character)

• You can also use the functions defined in


<cstring>

13
(Optional)

Some Functions in <cstring>


Functions Operation

strlen(s) Obtain string length

strcpy(s1, s2) Copy string

strcat(s1, s2) Concatenate strings

strcmp(s1, s2) Compare two strings

… …

(s, s1, and s2 are of type “char *”)

14
(Optional)

<cstring> Example
1 #include <cstring>
2 #include <iostream>
3 using namespace std;
4
5 int main() {
6 char s1[10], s2[10] = "abc";
7
8 cout << strlen(s2) << endl; // Print 3
9
10 strcpy(s1, s2); // s1 becomes "abc"
11 cout << s1 << endl;
12
13 strcat(s1, "def"); // s1 becomes "abcdef"
14 cout << s1 << endl;
15 3
16 return 0; abc
17 } abcdef 15
Class string
• Hides the actual representation of a string from the
programmers
• Offers many member functions for manipulating
strings
• Makes manipulating strings and managing storage for
strings easier than using arrays
Programmers
length() append(…) at(…) compare(…)
manipulate a
substr(…) insert(…) find(…) … string object
H E L L O ˽ W O R L D \0 through public
0 1 2 3 4 5 6 7 8 9 10 11 member functions
16
Declaring and Initializing string
Objects Using Constructors
1 #include <iostream>
2 #include <string> // Need to include header <string>
3 using namespace std;
4 Remember to add this!
5 int main() {
6 // If not initialized, holds the empty string ""
7 string s1;
8
9 string s2("Hello"); // s2 holds the string "Hello"
10
11 string s3(s2); // s3 holds what s2 is holding
12
13 cout << s1 << "_" << s2 << "_" << s3 << endl;
14
15 return 0;
16 }
_Hello_Hello 17
Some Constructors of string
1. string();
Simply treat it as
2. string( const string &str ); unsigned long
3. string( size_t n, char c );
4. string( const char *str );
5. string( const char *str, size_t n );
1 string str1( 5, 'c' ); // 3
2 string str2( "Now is the time..." ); // 4
3 string str3( "Now is the time...", 8 ); // 5
4 cout << str1 << endl;
ccccc
5 cout << str2 << endl;
Now is the time...
6 cout << str3 << endl;
Now is t
Reference: https://cplusplus.com/reference/string/string/string/ 18
String Assignment and Passing
string Objects to Function
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 void foo(string str) {
6 cout << str << endl;
7 }
8
9 int main() { Copy the content of s2 to s1
10 string s1, s2("XYZ");
11 Use operator = to copy a
12 s1 = s2; c-string to a string object
13 s2 = "ABC";
14
15 foo(s1);
Pass-by-value: copy from the
16 foo("DEF"); argument to the parameter as
17 return 0; “parameter = argument;” XYZ
18 } DEF 19
String Concatenation
• Operators + and += are overloaded for string objects
to support string concatenation

• + usage: lhs + rhs


• lhs and rhs can be a string object, a c-string, or a char
• But either lhs or rhs must be a string object

• += usage: lhs += rhs


• lhs must be a string object
• rhs can be a string object, a c-string, or a char

• Result of concatenation is a string object


20
String Concatenation: Examples
1 string s1("ABC"), s2("XYZ"), s3;
2
3 s3 = s1 + s2; // s3 holds "ABCXYZ"
4 s3 = s1 + "DEF"; // s3 holds "ABCDEF"
5 s3 = s1 + " " + s2; // s3 holds "ABC XYZ"
6
7 s3 = s1 + 'A'; // s3 holds "ABCA"
8
9 s3 = "ABC " + "DEF"; /* Compilation error. Neither
10 operand is a string object */
11 s3 = s1 + " " + "DEF"; // s3 holds "ABC DEF"
12 cout << ("[" + s2 + "]") << endl; // Print [XYZ]
13 s3 = "DEF" + " " + s1; // Compilation Error. Why?
14
15 // string("DEF") creates an anonymous string object
16 s3 = "ABC" + string("DEF"); // s3 holds "ABCDEF"
17
18 s1 += "XYZ"; // s1 holds "ABCXYZ" 21
Member Function length()
• Member function length() returns the number
of characters in a string object as an unsigned
integer

• E.g.: string s1("Hello");


string s2("\n\n\n");

int len1 = (int)s1.length();


cout << len1 << endl; // Print 5
cout << s2.length() << endl; // Print 3
cout << (s1+s2).length() << endl; // Print 8

Calls the member function of the


concatenated (anonymous) string object 22
Accessing Individual Characters
string str("Hello World");
H e l l o ˽ W o r l d
Index 0 1 2 3 4 5 6 7 8 9 10
• Each character in a string can be identified by an index
• Indexes run from 0 to str.length() – 1
• Member function at(i) to access the character at
index i
string str("Hello World");

cout << str.at(1) << endl; // Print 'e'


str.at(5) = '-'; // Replace ' ' by '-'
cout << str << endl; // Print Hello-World

The member-function call can even be on the LHS of assignment (=) 23


Accessing Individual Characters
string str("Hello World");
H e l l o ˽ W o r l d
Index 0 1 2 3 4 5 6 7 8 9 10
• Using an index not in the range 0 …
str.length() – 1 results in runtime error
• E.g.:
string str("Hello World");

int len = (int)str.length();


str.at(-1) = 'A'; // Runtime error
cout << str.at(len); // Runtime error

24
Accessing Individual Characters:
Array Subscript Notation
string str("Hello World");
H e l l o ˽ W o r l d
Index 0 1 2 3 4 5 6 7 8 9 10
• Operator [] is overloaded to provide alternative
style for accessing individual characters
• E.g.:
string str("Hello World");
str[i] is equivalent
int len = (int)str.length(); to str.at(i) if i is
str[4] = 'h'; in proper range
cout << str[len - 3];

Using member function at(…) is safer because it would


throw an exception (alert you) if the index is out of bounds 25
Accessing Characters: Example
1 string s("ABCDEFGHIJ");
2 int i;
3
4 // Print one character per line
5 for (i = 0; i < s.length(); i++)
6 cout << s[i] << endl;
7
8 // Print characters in reverse order
9 for (i = s.length() - 1; i >= 0; i--)
10 cout << s.at(i);
11
12 // Reverse the order of the characters
13 int len = (int)s.length();
14 char tmp;
15 for (i = 0; i < len / 2; i++) {
16 tmp = s[i];
17 s[i] = s[len - i - 1];
18 s[len - i - 1] = tmp;
19 } 26
String Input Using cin
• >> can read strings separated by whitespace
characters

• E.g.: string s1, s2;


cout << "Enter a sentence: ";
cin >> s1 >> s2;
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
Enter a sentence: Hello, how are you?↵
s1 = Hello,
s2 = how

27
String Input Using Function
getline()
• Read a string from an input stream object until a
newline character is encountered
• The new line character is consumed from the input
stream but is not included in the string object

• E.g.: string s1;


cout << "Enter a sentence: ";
getline(cin, s1);
cout << "s1 = " << s1 << endl;
cout << "length = " << s1.length() << endl;
Enter a sentence: Hello, how are you?↵
s1 = Hello, how are you?
length = 19
28
Example
1 string str;
2 char ch;
3
4 cout << "Enter a string: ";
5 getline(cin, str);
6
7 for (int i = 0; i < str.length(); i++) {
8
9 ch = str.at(i);
10 if (ch >= 'A' && ch <= 'Z')
11 str.at(i) = ch - 'A' + 'a'; // Upper-to-lowercase
12 else if (ch >= 'a' && ch <= 'z')
13 str.at(i) = ch - 'a' + 'A'; // Lower-to-uppercase
14
15 }
16 cout << str << endl;
Convert the characters in a Enter a string: Hello! How are You?↵
user input from uppercase to hELLO! hOW ARE yOU?
lowercase and vice versa 29
String Comparison
• The comparison operators (<, >, <=, >=, ==, !=) are
also overloaded to support string comparison

• One of the operands must be a string object.


(The other operand can be c-string or string
object)

• How is the order between two strings decided?


• “ABC” > “abc” true/false?
• “ABC” == “abc” true/false?
• “=^.^=” > “>.<” true/false?

30
Strings are Compared
Lexicographically
s1 A B C D

s2 A B C X Y

• Compare the 1st character of each string, then the


2nd character of each string, and so on, until a
difference can be made

• ASCII value determines the order of the characters


• ASCII value of ‘X’ is 88
• ASCII value of ‘D’ is 68

31
String Comparison: Examples
1 string s1("ABC"), s2("XYZ");
2
3 // Print 0 or 1?
4 cout << (s1 > s2) << endl;
5
6 // Print 0 or 1?
7 cout << (s1 == "ABC") << endl;
8
9 // Print 0 or 1?
10 cout << (s1 + "XYZ" == "ABC" + s2) << endl;
11
12 // Improper comparison. Why?
13 if ("ABC" < "ABCD") {
14 …
15 }
32
String Comparison: Exercises
string s1(…) string s2(…) >, ==, or <?
"ABC" "ABCDE"
"XYZ" "abc"
"AB C" "ABC"
"" "ABC"
"13145" "013145"

ASCII of some characters:


• ‘ ’ (space): 32
• ‘0’ – ‘9’: 48 – 57
• ‘A’ – ‘Z’: 65 – 90
• ‘a’ – ‘z’: 97 – 122
33
Summary
• Declare and initialize string objects

• Find out string length

• Manipulate characters in string objects

• Read strings from the user

• Understand lexicographic comparison of strings

Next: Stream I/O and File I/O

34
(Optional)

Other string Member Functions


• insert(): Insert a substring into a string object
• erase(): Erase a substring from a string object
• replace(): Replace a substring in a string object
• find(), rfind(): Locate a substring from a string
object
• substr(): Retrieve a substring from a string object
• c_str(): Retrieve an equivalent c-string from a
string object
• and many more…
See: https://cplusplus.com/reference/string/string/
35
(Optional)

find(): Example
• find(): returns the starting position of the
located substring, or returns -1 if the target string
cannot be found
1 string s1("01234 0123");
2 size_t pos; // size_t is simply unsigned int
3
4 // Locate "123" in s1 starting from position 0
5 pos = s1.find("123"); // pos gets 1
6
7 // Locate "123" in s1 starting from position 2
8 pos = s1.find("123", 2); // pos gets 7
9
10 // Check if a substring exists in a string
11 if (s1.find("ABC") == -1) {
12 … // "ABC" does not exist in s1
13 } 36
substr() and c_str(): (Optional)

Examples
• substr(): returns a “substring” (a portion of a
string) from one position to another position
• c_str(): returns an equivalent c-string from a
string object
1 string s1, s2("0123456789");
2 char *s3;
3
4 /* A substring that starts at position 2 and includes 4
5 characters */
6 s1 = s2.substr(2, 4); // s1 becomes "2345"
7
8 /* A substring that starts at position 4 and includes up to
9 the last character */
10 s1 = s2.substr(4); // s1 becomes "456789"
11
12 s3 = s1.c_str(); // s3 is c-string equivalent to s1 37
(Optional)

insert(): Example
1 string s1, s2("XYZ");
2
3 s1 = "012345";
4 // Insert s2 at position 2 of s1
5 s1.insert(2, s2); // s1 becomes "01XYZ2345"
6
7 s1 = "012345";
8 // Insert "ABC" at position 0 of s1
9 s1.insert(0, "ABC"); // s1 becomes "ABC012345"
10
11 s1 = "012345";
12 s2 = "ABCDEF";
13 /* Insert "BCDE" at position 3 of s1. The 3rd parameter, 1,
14 indicates the start of the substring in s2. The 4th
15 parameter, 4, is the length of the substring in s2 */
16 s1.insert(3, s2, 1, 4); // s1 becomes "012BCDE345" 38
(Optional)

Converting Strings to Numbers:


Example
1 #include <string> // For using stoi(), stod()
2 …
3 int main() { stoi(), stod(): Since C++11
4 int num;
5 num = stoi("123"); // num becomes 123
6 num = stoi("-456"); // num becomes -456
7
8 /* stoi() stops processing as soon as it encounters a
9 symbol that cannot be part of an integer */
10 num = stoi("12x456"); // num becomes 12
11 num = stoi("xxx123"); // No conversion. Runtime error!
12
13 double d;
14 d = stod("123"); // d becomes 123.0
15 d = stod("-1.34e-2"); // d becomes -0.0134
16 d = stod("123.321.123"); // d becomes 123.321
17 … 39
(Optional)

Command-Line Arguments
• Command-line arguments allow users to pass data
to a program from the OS at run-time

Command-line arguments
Program name

• Advantage of using command-line arguments:


• Allow a program to execute without user interaction, but
still with varying program behavior
• Reading command-line arguments into a C++
program requires the use of c-strings 40
(Optional)

Command-Line Arguments
• Command-line arguments are stored as an array of
c-strings in C++
Have to modify the int argc: number of
main() function header command-line arguments
1 #include <iostream>
2 using namespace std;
char-pointers
3 (c-strings)
4 int main(int argc, char *argv[]) {
5 cout << "Num of command-line arg: " << argc << endl;
6 for (int i = 0; i < argc; i++) {
7 cout << "Arg #" << i << ": " << argv[i] << endl;
8 }
9 return 0; Array of char-pointers
10 } (Array of c-strings) 41
(Optional)
Parsing Command-Line Index argv
0 “example.exe”
Arguments 1 “ab”
1 #include <iostream> 2 “cdef”
2 using namespace std; 3 “123”
3 4 “@#$%”
4 int main(int argc, char *argv[]) {
5 cout << "Num of command-line arg: " << argc << endl;
6 for (int i = 0; i < argc; i++) {
7 cout << "Arg #" << i << ": " << argv[i] << endl;
8 }
9 return 0;
10 }

Program name (argv[0])


is counted as one of the
command-line arguments 42
(Optional)
Command-Line Argument: Example
1 #include <iostream>
2 #include <string> // For using stoi()
3 using namespace std;
4
5 int main(int argc, char *argv[]) {
6 int sum = 0, x;
7 for (int i = 1; i < argc; i++) {
8 x = stoi(argv[i]);
9 sum += x;
10 }
11 cout << "Sum = " << sum << endl;
12 return 0;
You can use "…" to
13 }
enclose an argument
that contains spaces

43

You might also like