0% found this document useful (0 votes)
2 views39 pages

Lesson 04 - 03. Functions, Arrays, Strings and Parameter Passing - 02

This document covers the concepts of functions, arrays, strings, and parameter passing in C++, focusing on C-style strings and their manipulation using various functions from the cstring library. It also introduces the string class and vector class for easier string and array handling. The document includes examples and exercises for practical understanding of these concepts.

Uploaded by

chi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views39 pages

Lesson 04 - 03. Functions, Arrays, Strings and Parameter Passing - 02

This document covers the concepts of functions, arrays, strings, and parameter passing in C++, focusing on C-style strings and their manipulation using various functions from the cstring library. It also introduces the string class and vector class for easier string and array handling. The document includes examples and exercises for practical understanding of these concepts.

Uploaded by

chi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

04.

Functions, Arrays,
Strings and Parameter
Passing - 02
Oritented Object Programming C++: Chapter 03

 Time: 120 Minutes


 Instructor: Hieu, Vo Dinh
 Email:[email protected]
 Phone: 0941 891 998
Previous lessons

1. Introduction C++
 C++ vs C
 Compilers, IDEs
2. C++ Language Basics
 Types: int, float, double
 Control Structures: if, for, while, switch-case…
3. Functions, Arrays, Strings and Parameter Passing-1
 void func(int);
 void func(int, int);
 void func(int, int = 2);
 int arr[10], brr[2][3];

Duy Tan University


2
Contents

V. C-style string
 C-style string likes string in C
 Example use C-style string in many ways
VI. cstring functions
 Use function in cstring library
VII.string class
 How to use string class like cstring in easy way
VIII.vector class
 Vector is type which likes the array
 How to use the vector class
Duy Tan University
3
V. C-style string

 A string is a sequences of characters, called C-Style


string in C++
 Example:
char name[10];

0 1 2 3 4 5 6 7 8 9

 The end of strings represented by the null character


Whose literal value can be written as '\0'

T H U Y \0
0 1 2 3 4 5 6 7 8 9

Duy Tan University


4
Initialization C-style strings

 Initialization C-style strings:


char name1[10] = { 'T', 'H', 'U', 'Y', '\0'
};
T H U Y \0
0 1 2 3 4 5 6 7 8 9

char name2[] = { 'T', 'H', 'U', 'Y', '\


0' }; The string literals
T H U Y \0
char
0
name3[]
1 2
= 3"THUY";
4

char
T name4[]
h u = "Thuy y";\0
0 1 2 3 4 5

Duy Tan University


5
Initialization C-style strings (cont.)

 Example:
char name1[10] = { 'T', 'H', 'U', 'Y', '\0'
};
char name2[] = { 'T', 'H', 'U', 'Y', '\
0' };
char name3[] = "THUY";
char name4[] = "Thu y";
char name5[10];
char name6[10] = name1; // Error
char name7[]; // Error
name5 = { 'T', 'H', 'U', 'Y', '\0' }; //
Error
name5 = "THUY"; 6
//
Duy Tan University
C-style string Input/ouput

 Use cout to print a c-style string


 char name[10] = {'T','H','U','Y','\0'};
 cout << name;
 Get a word:
 char name[10];
 cin >> name;
 Get a line:
 char address[100]
 cin.getline(address, 100);
 cin.getline(address, 100, '\n');

Duy Tan University


7
C-style string Input/ouput (cont.)

 Example 01: Output


Address: 254 Nguyen Van Linh
#include <iostream>
Name: Thuy
using namespace std;- Address: 254 Nguyen Van Linh
int main() { - Name: Thuy
char name[10];
char address[100];
cout << "Address: ";
cin.getline(address, 100);
cout << "Name: ";
cin >> name;
cout << "- Address: " << address << endl;
cout << "- Name: " << name << endl;
return 0;
} 8 Duy Tan University
C-style string Input/ouput (cont.)

 Example 01: Output


Address:
#include <iostream>
using namespace std;
int main() {
char name[10];
char address[100];
cout << "Address: ";
cin.getline(address, 100);
cout << "Name: ";
cin >> name;
cout << "- Address: " << address << endl;
cout << "- Name: " << name << endl;
return 0;
} 9 Duy Tan University
C-style string Input/ouput (cont.)

 Example 01: Output


Address: 254 Nguyen Van Linh
#include <iostream>
using namespace std;
int main() {
char name[10];
char address[100];
cout << "Address: ";
cin.getline(address, 100);
cout << "Name: ";
cin >> name;
cout << "- Address: " << address << endl;
cout << "- Name: " << name << endl;
return 0;
} 10 Duy Tan University
C-style string Input/ouput (cont.)

 Example 01: Output


Address: 254 Nguyen Van Linh
#include <iostream>
Name:
using namespace std;
int main() {
char name[10];
char address[100];
cout << "Address: ";
cin.getline(address, 100);
cout << "Name: ";
cin >> name;
cout << "- Address: " << address << endl;
cout << "- Name: " << name << endl;
return 0;
} 11 Duy Tan University
C-style string Input/ouput (cont.)

 Example 01: Output


Address: 254 Nguyen Van Linh
#include <iostream>
Name: Thuy
using namespace std;
int main() {
char name[10];
char address[100];
cout << "Address: ";
cin.getline(address, 100);
cout << "Name: ";
cin >> name;
cout << "- Address: " << address << endl;
cout << "- Name: " << name << endl;
return 0;
} 12 Duy Tan University
C-style string Input/ouput (cont.)

 Example 01: Output


Address: 254 Nguyen Van Linh
#include <iostream>
Name: Thuy
using namespace std;- Address: 254 Nguyen Van Linh
int main() { - Name: Thuy
char name[10];
char address[100];
cout << "Address: ";
cin.getline(address, 100);
cout << "Name: ";
cin >> name;
cout << "- Address: " << address << endl;
cout << "- Name: " << name << endl;
return 0;
} 13 Duy Tan University
C-style string Input/ouput (cont.)

 Example 02: Output


#include <iostream> Name: Thuy
using namespace std;Address: - Name: Thuy
int main() { - Address:
char name[10];
char address[100];
cout << "Name: ";
cin >> name;
cout << "Address: ";
cin.getline(address, 100);
cout << "- Name: " << name << endl;
cout << "- Address: " << address << endl;
return 0;
} 14 Duy Tan University
C-style string Input/ouput (cont.)

Example 03: Solve the problem on Example 02


char name[10]; Output
char address[100];Name: Thuy
Address: 254 Nguyen Van Linh
cout << "Name: ";
- Name: Thuy
cin >> name; - Address: 254 Nguyen Van Linh
cout << "Address: ";
do {
cin.getline(address, 100);
} while (address[0] == '\0');
cout << "- Name: " << name << endl;
cout << "- Address: " << address << endl;

Duy Tan University


15
Contents

V. C-style string
 C-style string likes string in C
 Example use C-style string in many ways
VI. cstring functions
 Use function in cstring library
VII.string class
 How to use string class like cstring in easy way
VIII.Vector class
 Vector is type which likes the array
 How to use the vector class
Duy Tan University
16
Practices by Your-Self in 5 minutes

Duy Tan University


17
Example 01

 Enter a string, print the string length.


int main() {
char str[100];
cout << "Enter a string: ";
cin.getline(str, 100);
int length = 0;
while (str[length] != '\0') {
length++;
}
cout << "The string length: " <<
length;
return 0;
} 18 Duy Tan University
Example 02

 Write a function to copy a c-style string to another


void myStrcpy(char destination[], char
source[]);
int main() { Output
char strA[100]; strA: Hello DTU
char strB[] = "Hello DTU"; strB: Hello DTU
myStrcpy(strA, strB);
cout << "strA: " << strA << endl;
cout << "strB: " << strB << endl;
return 0;
}

Duy Tan University


19
Example 02 (cont.)

 The solution:
void myStrcpy(char destination[], char
source[]) {
int i = 0;
while (source[i] != '\0') {
destination[i] = source[i];
i++;
}
destination[i] = '\0';
}

Duy Tan University


20
VI. cstring functions

 #include <cstring>
cstring or string.h defines functions to manipulate
C-style strings and arrays
http://www.cplusplus.com/reference/cstring/
 Get string length:
unsigned int strlen(const char* str);
 Copy string:
char* strcpy(char* destination, const char*
source);
 Copy characters from string:
char* strncpy(char* destination, const char*
source, unsigned int num);
Duy Tan University
21
VI. cstring functions (cont.)

 Concatenate strings:
char* strcat(char* destination, const char*
source);
 Append characters from string:
char* strncat(char* destination, const char*
source, unsigned int num);
 Compare two strings:
int strcmp(const char* str1, const char* str2);
 Compare characters of two strings:
int strncmp(const char* str1, const char* str2,
unsigned int num);
 Note: some compilers required: strcat_s, strncat_s,..
Duy Tan University
22
Example 01:
Output
#include <iostream>
str1: Hello DTU
#include <cstring> strlen(str1): 9
using namespace std; strcpy(str3, str1): Hello DTU
int main() { strncpy(str2, str1, 4): Hell56789
char str1[100] = "Hello DTU";
char str2[100] = "123456789", str3[100];
cout << "str1: " << str1 << endl;
cout << "strlen(str1): " << strlen(str1) <<
endl;
strcpy(str3, str1); // Copy str1 to str3
cout << "strcpy(str3, str1): " << str3 << endl;
strncpy(str2, str1, 4);//Copy 4 chars to str2
cout << "strncpy(str2, str1, 4): " << str2;
return 0; Duy Tan University
23
Example 02:
#include <iostream> Output

#include <cstring> strcat(str1, str2): HelloCMU-CS OOP C++


strncat(str3, str2, 3): StudentsCMU
using namespace std;
int main() {
char str1[100] = "Hello";
char str2[100] = "CMU-CS OOP C++";
char str3[100] = "Students";
strcat(str1, str2);
cout << "strcat(str1, str2): " << str1 <<
endl;
strncat(str3, str2, 3);
cout << "strncat(str3, str2, 3): " << str3;
return 0;
} Duy Tan University
24
Example 03:
#include <iostream> Output
#include <cstring> str1 > str2
using namespace std;
int main() {
char str1[100] = "Hello";
char str2[100] = "Hallo";
if (strcmp(str1, str2) == 0) {
cout << "str1 and str2 are the same" << endl;
} else if (strcmp(str1, str2) > 0) {
cout << "str1 > str2" << endl;
} else if (strcmp(str1, str2) < 0) {
cout << "str1 > str2" << endl;
}
return 0;
}
Duy Tan University
25
Contents

V. C-style string
 C-style string likes string in C
 Example use C-style string in many ways
VI. cstring functions
 Use function in cstring library
VII.string class
 How to use string class like cstring in easy way
VIII.Vector class
 Vector is type which likes the array
 How to use the vector class
Duy Tan University
26
VII. string class

 #include <string>
Defines std::string class to represent sequence of
characters as an object of class.
string class defined functions to operate on strings.
www.cplusplus.com/reference/string/string/
 Initialization string objects:
 string s0;
 string s1 = "Hello DTU";
 string s2("Hello DTU");
 string s3 = s1;
 char str[] = "DTU students";
 string s4 = str;
Duy Tan University
27
VII. string class (cont.)
string class <string> C string <cstring>
string s1 = "Hello"; char c1[100] =
string s2 = "DTU"; "Hello";
string s3; char c2[] = "DTU";
char c3[100];
cin >> s3; cin >> c3;
getline(cin, s3); cin.getline(c3, 100);
s1.length(); strlen(c1);
s3 = s1; strcpy(c3, c1);
s3 = s1.substr(0, 2); strncpy(c3, c1, 2);
s3 = s3 + s2; strcat(c3, c2);
s3 += s2.substr(0,2); strncat(c3, c2, 2);
s1>s2; s1==s2; s1<s2; strcmp(c2, c3);
Duy Tan University
28
Output
Example 01 First name: Thuy
Last name: Tran
#include <iostream> Class room: 702 NVL
#include <string> Thuy Tran
using namespace std; Learn in 702 NVL
int main() {
string firstName, lastName, classRoom;
cout << "First name: "; cin >> firstName;
cout << "Last name: "; cin >> lastName;
cout << "Class room: ";
do {
getline(cin, classRoom);
} while (classRoom.length() == 0);
cout << firstName + " " + lastName << endl;
cout << "Learn in " << classRoom;
return 0;
}
Duy Tan University
29
Example 02
void toUpperCase(string str) {
for (int i = 0; i < str.length(); i++) {
if ('a' <= str[i] && str[i] <= 'z')
str[i] = str[i] - 32;
}
}
int main() { Output
string s = "Viet Nam"; Viet Nam
toUpperCase(s);
cout << s << endl;
return 0;
}

Duy Tan University


30
Example 03
string toUpperCase(string str) {
for (int i = 0; i < str.length(); i++) {
if ('a' <= str[i] && str[i] <= 'z')
str[i] = str[i] - 32;
}
return str;
} Output
int main() { VIET NAM
string s = "Viet Nam";
s = toUpperCase(s);
cout << s << endl;
return 0;
}
Duy Tan University
31
Contents

V. C-style string
 C-style string likes string in C
 Example use C-style string in many ways
VI. cstring functions
 Use function in cstring library
VII.string class
 How to use string class like cstring in easy way
VIII.Vector class
 Vector is type which likes the array
 How to use the vector class
Duy Tan University
32
VIII. Vector class

 #include <vector>
Defines the vector container class
Vectors are sequence containers representing
arrays that can change in size.
 Declaration for an vector: vector<type> name;
 Example:
vector<int> intVt;
vector<float> floatVt;
vector<string> stringVt;

Duy Tan University


33
VIII. Vector class (cont.)

 Initializing vectors:
 vector<int> vt1;
 vector<int> vt2(4, 8);
0 1 2 3
vt2 8 8 8 8

 vector<int> vt3(vt2);
0 1 2 3
vt3 8 8 8 8

Duy Tan University


34
VIII. Vector class (cont.)

 Add element at the end


 void push_back(const value_type& val);
 Delete last element
 void pop_back();
 Get the number of elements in the vector
 unsigned int size() const;
 Test whether vector is empty
 bool empty() const;
 Removes all elements from the vector
 void clear();

Duy Tan University


35
VIII. Vector class (cont.)
vector<int> vt;
vt.push_back(9);
vt.push_back(1);
vt.push_back(7);
cout << "size(): " << vt.size() << endl;
vt.pop_back();
cout << "size(): " << vt.size() << endl;
cout << (vt.empty() ? "Empty" : "Not empty")<<
endl;
vt.clear();
cout << "size(): " << vt.size() << endl;
cout << (vt.empty() ? "Empty" : "Not empty")<<
endl;

Duy Tan University


36
Contents

V. C-style string
 C-style string likes string in C
 Example use C-style string in many ways
VI. cstring functions
 Use function in cstring library
VII.string class
 How to use string class like cstring in easy way
VIII.Vector class
 Vector is type which likes the array
 How to use the vector class
Duy Tan University
37
Summary

 C-style string is a sequences of characters, likes C


 cstring library defined functions for C-style string
 string class likes C-string, but easy to using
 vector class likes array, but better to using

Duy Tan University


38
Problems (cont.)

1. Cstring library has functions: strlen, strcpy, strcmp.


Re-write the functions by your-self,
2. Write countWords(…) to count how many words
have in a string an return the quality.
3. Write displayBinary(…) to display the binary of an
integer.
4. Write functions to get two big numbers (the length >
100), and add the number together, show the result.

Duy Tan University


39

You might also like