េមេរៀនទី៨ String ៨.១ និយមន័យ កនុងC++ string ជករ្របមូលផ្ដំុតួអក រ(characters)ចូលគន បេងកើតបនជពកយ ឬឃ្ល ្របេយគេផ ងៗ េហើយ្រតូវអមេ យសញញ " .... "។ Header file <string> ្រតូវបនយកមកេ្របើ េដើមបីេធ្វើករ្របតិបត្តិជមួយ string។ ៨.២ ការរបកាស string (string declaration) កនុងC++ string ចេធ្វើករ្របកសបនពីររេបៀបគឺ៖ រេបៀបទី១៖ ្រតូវបនបេងកើតេឡើងដូចជ array of character មន្របែវង និង្រតូវបន បញចប់េ យសញញ '\0' េ ថ NULL។ char string_name[lenght]; ឧទហរណ៍៖ char line[80]; - line មន្របែវងអតិបរ ិម ចផទុកបន 80 characters char line[80]="Hello Cambodia"; កនុងរេបៀបទី១ េពលបញូច លទិននន័យ េគេ្រចើនេ្របើជមួយkeyword cin.getline របស់ iostream។ រេបៀបៃនករសរេសរគឺៈ cin.getlin(string_name,lenght)។ #include <iostream> using namespace std; int main () { char name[256], title[256]; cout << "Please, enter your name: "; cin.getline (name,256); cout << "Please, enter your favourite movie: ";cin.getline (title,256); cout << name << "'s favourite movie is " << title; return 0;} រេបៀបទី២៖ string string_name; #include <iostream> #include <string> using namespace std; int main () { string question = "Where do you live? "; string answer; cout << question; getline(cin,answer); cout << " I live in " << answer << "\n"; return 0; } ៨.៣ ការរបតិបតតិការជាមួយstring េយើង ច្របតិបត្តិករជមួយstring ដូចគនេទ array ែដរ គឺ េឈមះអេថរ string និងមន [index] េនពីេ្រកយ។ ៨.៤ Functions មួយចំនួនរបស់ string Sr.No Function & Purpose strcpy(s1, s2); 1 Copies string s2 into string s1. strcat(s1, s2); 2 Concatenates string s2 onto the end of string s1. strlen(s1); 3 Returns the length of string s1. strcmp(s1, s2); 4 Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. strchr(s1, ch); 5 Returns a pointer to the first occurrence of character ch in string s1. strstr(s1, s2); 6 Returns a pointer to the first occurrence of string s2 in string s1. #include <string> using namespace std; int main () { string str1 = "Hello"; string str2 = "World"; string str3; int len ; // copy str1 into str3 str3 = str1; cout << "str3 : " << str3 << endl; // concatenates str1 and str2 str3 = str1 + str2; cout << "str1 + str2 : " << str3 << endl; // total length of str3 after concatenation len = str3.size(); cout << "str3.size() : " << len << endl; return 0; }