Lesson 04 - 03. Functions, Arrays, Strings and Parameter Passing - 02
Lesson 04 - 03. Functions, Arrays, Strings and Parameter Passing - 02
Functions, Arrays,
Strings and Parameter
Passing - 02
Oritented Object Programming C++: Chapter 03
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];
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
0 1 2 3 4 5 6 7 8 9
T H U Y \0
0 1 2 3 4 5 6 7 8 9
char
T name4[]
h u = "Thuy y";\0
0 1 2 3 4 5
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
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
The solution:
void myStrcpy(char destination[], char
source[]) {
int i = 0;
while (source[i] != '\0') {
destination[i] = source[i];
i++;
}
destination[i] = '\0';
}
#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
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;
}
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;
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
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