logo

كيفية تقسيم السلاسل في C++؟

سيناقش هذا الموضوع كيف يمكننا تقسيم السلاسل المعطاة إلى كلمة واحدة في ملف لغة برمجة سي++ . عندما نقسم مجموعة من الكلمات أو مجموعات السلاسل إلى كلمات مفردة، يطلق عليها اسم ينقسم أو تقسيم السلسلة. ومع ذلك، لا يمكن تقسيم السلاسل إلا باستخدام بعض المحددات مثل المسافة البيضاء ()، والفاصلة (،)، والواصلة (-)، وما إلى ذلك، مما يجعل الكلمات فردية. علاوة على ذلك، لا توجد وظيفة تقسيم محددة مسبقًا لتقسيم مجموعة السلاسل إلى سلسلة فردية. لذا، سنتعلم هنا الطرق المختلفة لتقسيم السلاسل إلى سلسلة واحدة في لغة C++.

كيفية تقسيم السلاسل في C++

طريقة مختلفة لتحقيق تقسيم السلاسل في C++

  1. استخدم الدالة strtok() لتقسيم السلاسل
  2. استخدم وظيفة Split() المخصصة لتقسيم السلاسل
  3. استخدم الدالة std::getline() لتقسيم السلسلة
  4. استخدم الدالة find() وsubstr() لتقسيم السلسلة

استخدم الدالة strtok() لتقسيم السلاسل

سترتوك (): يتم استخدام الدالة strtok() لتقسيم السلسلة الأصلية إلى أجزاء أو رموز مميزة بناءً على المحدد الذي تم تمريره.

بناء الجملة

 char *ptr = strtok( str, delim) 

في بناء الجملة أعلاه، تحتوي الدالة strtok() على معلمتين، وهما شارع ، و ال أشارك .

شارع : السلسلة هي سلسلة أصلية تقسم منها الدالة strtok()‎ السلاسل.

تبديل جافا

أشارك : إنه حرف يستخدم لتقسيم سلسلة. على سبيل المثال، الفاصلة (،)، المسافة ()، الشرطة (-)، إلخ.

يعود : يقوم بإرجاع مؤشر يشير إلى الرموز المميزة للأحرف التالية. في البداية، يشير إلى الرمز الأول للسلاسل.

ملاحظة: تقوم الدالة strtok() بتعديل السلسلة الأصلية وتضع حرف NULL ('') في موضع المحدد في كل استدعاء للدالة strtok(). وبهذه الطريقة، يمكن بسهولة تتبع حالة الرمز المميز.

برنامج لتقسيم السلاسل باستخدام الدالة strtok()

لنفكر في مثال لتقسيم السلسلة في لغة C++ باستخدام الدالة strtok().

اجمل ابتسامة في العالم

Program.cpp

 #include #include using namespace std; int main() { char str[100]; // declare the size of string cout &lt;&lt; &apos; Enter a string: &apos; &lt;<endl; cin.getline(str, 100); use getline() function to read a string from input stream char *ptr; declare ptr pointer ' , '); strtok() separate using comma (,) delimiter. cout << 
 split function: endl; while loop check is not null (ptr !="NULL)" { print the token (null, } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a string: Learn how to split a string in C++ using the strtok() function. Split string using strtok() function: Learn how to split a string in C++ Using the strtok() function. </pre> <h3>Program to use custom split() function to split strings</h3> <p>Let&apos;s write a program to split sequences of strings in C++ using a custom split() function.</p> <p> <strong>Program2.cpp</strong> </p> <pre> #include #include #define max 8 // define the max string using namespace std; string strings[max]; // define max string // length of the string int len(string str) { int length = 0; for (int i = 0; str[i] != &apos;&apos;; i++) { length++; } return length; } // create custom split() function void split (string str, char seperator) { int currIndex = 0, i = 0; int startIndex = 0, endIndex = 0; while (i <= len(str)) { if (str[i]="=" seperator || i="=" endindex="i;" string substr ; substr.append(str, startindex, - startindex); strings[currindex]="subStr;" currindex +="1;" startindex="endIndex" 1; } i++; int main() str="Program to split strings using custom split function." char space split(str, seperator); cout <<' the split is: '; for (int < max; i++) << '
 : ' strings[i]; return 0; pre> <p> <strong>Output</strong> </p> <pre> The split string is: i : 0 Program i : 1 to i : 2 split i : 3 strings i : 4 using i : 5 custom i : 6 split i : 7 function. </pre> <h3>Use std::getline() function to split string</h3> <p>A getline() function is a standard library function of C++ used to read the string from an input stream and put them into the vector string until delimiter characters are found. We can use <strong>std::getline()</strong> function by importing the header file.</p> <p> <strong>Syntax</strong> </p> <pre> getline(str, token, delim); </pre> <p>It has three parameters:</p> <p> <strong>str:</strong> A str is a variable that stores original string.</p> <p> <strong>token:</strong> It stores the string tokens extracted from original string.</p> <p> <strong>delim:</strong> It is a character that are used to split the string. For example, comma (,), space ( ), hyphen (-), etc.</p> <h3>Program to use getline() function to split strings</h3> <p>Let&apos;s consider an example to split strings using the getline() function in C++.</p> <p> <strong>Program3.cpp</strong> </p> <pre> #include #include #include #include using namespace std; int main() { string S, T; // declare string variables getline(cin, S); // use getline() function to read a line of string and store into S variable. stringstream X(S); // X is an object of stringstream that references the S string // use while loop to check the getline() function condition while (getline(X, T, &apos; &apos;)) { /* X represents to read the string from stringstream, T use for store the token string and, &apos; &apos; whitespace represents to split the string where whitespace is found. */ cout &lt;&lt; T &lt;&lt; endl; // print split string } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Welcome to the JavaTpoint and Learn C++ Programming Language. Welcome to the JavaTpoint and Learn C++ Programming Language. </pre> <h3>Program to split the given string using the getline() function</h3> <p>Let&apos;s consider an example to split a given string in C++ using the getline() function.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include #include #include void split_str( std::string const &amp;str, const char delim, std::vector &amp;out ) { // create a stream from the string std::stringstream s(str); std::string s2; while (std:: getline (s, s2, delim) ) { out.push_back(s2); // store the string in s2 } } int main() { std:: string s2 = &apos;Learn How to split a string in C++&apos;; const char delim = &apos; &apos;; /* define the delimiter like space (&apos; &apos;), comma (,), hyphen (-), etc. */ std::cout &lt;&lt; &apos;Your given string is: &apos; &lt;&lt; s2; std::vector out; // store the string in vector split_str (s2, delim, out); // call function to split the string // use range based for loop for (const auto &amp;s2: out) { std::cout &lt;&lt; &apos;
&apos; &lt;&lt; s2; } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Your given string is: Learn How to split a string in C++ Learn How to split a string in C++ </pre> <h3>Use find() and substr() function to split strings</h3> <p>Let&apos;s write a program to use find() function and substr() function to split given strings in C++.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main() { // given string with delimiter string given_str = &apos;How_to_split_a_string_using_find()_and_substr()_function_in_C++&apos;; string delim = &apos;_&apos;; // delimiter cout &lt;&lt; &apos; Your string with delimiter is: &apos; &lt;&lt; given_str &lt;&lt; endl; size_t pos = 0; string token1; // define a string variable // use find() function to get the position of the delimiters while (( pos = given_str.find (delim)) != std::string::npos) { token1 = given_str.substr(0, pos); // store the substring cout &lt;&lt; token1 &lt;&lt; endl; given_str.erase(0, pos + delim.length()); /* erase() function store the current positon and move to next token. */ } cout &lt;&lt; given_str &lt;&lt; endl; // it print last token of the string. } </pre> <p> <strong>Output</strong> </p> <pre> Your string with delimiter is: How_to_split_a_string_using_find()_and_substr()_function_in_C++ How to split a string using find() and substr() function in C++ </pre> <p>In the above program, we use a <strong>find()</strong> function inside the loop to repeatedly find the occurrence of the delimiter in the given string and then split it into tokens when the delimiter occurs. And the <strong>substr()</strong> function stores the sub-string to be printed. On the other hand, an erase() function stores the current position of the string and moves to the next token, and this process continues until we have got all the split strings.</p> <hr></=></pre></endl;>

برنامج لاستخدام وظيفة تقسيم () مخصصة لتقسيم السلاسل

لنكتب برنامجًا لتقسيم تسلسلات السلاسل في لغة C++ باستخدام دالة Split()‎ مخصصة.

Program2.cpp

 #include #include #define max 8 // define the max string using namespace std; string strings[max]; // define max string // length of the string int len(string str) { int length = 0; for (int i = 0; str[i] != &apos;&apos;; i++) { length++; } return length; } // create custom split() function void split (string str, char seperator) { int currIndex = 0, i = 0; int startIndex = 0, endIndex = 0; while (i <= len(str)) { if (str[i]="=" seperator || i="=" endindex="i;" string substr ; substr.append(str, startindex, - startindex); strings[currindex]="subStr;" currindex +="1;" startindex="endIndex" 1; } i++; int main() str="Program to split strings using custom split function." char space split(str, seperator); cout <<\' the split is: \'; for (int < max; i++) << \'
 : \' strings[i]; return 0; pre> <p> <strong>Output</strong> </p> <pre> The split string is: i : 0 Program i : 1 to i : 2 split i : 3 strings i : 4 using i : 5 custom i : 6 split i : 7 function. </pre> <h3>Use std::getline() function to split string</h3> <p>A getline() function is a standard library function of C++ used to read the string from an input stream and put them into the vector string until delimiter characters are found. We can use <strong>std::getline()</strong> function by importing the header file.</p> <p> <strong>Syntax</strong> </p> <pre> getline(str, token, delim); </pre> <p>It has three parameters:</p> <p> <strong>str:</strong> A str is a variable that stores original string.</p> <p> <strong>token:</strong> It stores the string tokens extracted from original string.</p> <p> <strong>delim:</strong> It is a character that are used to split the string. For example, comma (,), space ( ), hyphen (-), etc.</p> <h3>Program to use getline() function to split strings</h3> <p>Let&apos;s consider an example to split strings using the getline() function in C++.</p> <p> <strong>Program3.cpp</strong> </p> <pre> #include #include #include #include using namespace std; int main() { string S, T; // declare string variables getline(cin, S); // use getline() function to read a line of string and store into S variable. stringstream X(S); // X is an object of stringstream that references the S string // use while loop to check the getline() function condition while (getline(X, T, &apos; &apos;)) { /* X represents to read the string from stringstream, T use for store the token string and, &apos; &apos; whitespace represents to split the string where whitespace is found. */ cout &lt;&lt; T &lt;&lt; endl; // print split string } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Welcome to the JavaTpoint and Learn C++ Programming Language. Welcome to the JavaTpoint and Learn C++ Programming Language. </pre> <h3>Program to split the given string using the getline() function</h3> <p>Let&apos;s consider an example to split a given string in C++ using the getline() function.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include #include #include void split_str( std::string const &amp;str, const char delim, std::vector &amp;out ) { // create a stream from the string std::stringstream s(str); std::string s2; while (std:: getline (s, s2, delim) ) { out.push_back(s2); // store the string in s2 } } int main() { std:: string s2 = &apos;Learn How to split a string in C++&apos;; const char delim = &apos; &apos;; /* define the delimiter like space (&apos; &apos;), comma (,), hyphen (-), etc. */ std::cout &lt;&lt; &apos;Your given string is: &apos; &lt;&lt; s2; std::vector out; // store the string in vector split_str (s2, delim, out); // call function to split the string // use range based for loop for (const auto &amp;s2: out) { std::cout &lt;&lt; &apos;
&apos; &lt;&lt; s2; } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Your given string is: Learn How to split a string in C++ Learn How to split a string in C++ </pre> <h3>Use find() and substr() function to split strings</h3> <p>Let&apos;s write a program to use find() function and substr() function to split given strings in C++.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main() { // given string with delimiter string given_str = &apos;How_to_split_a_string_using_find()_and_substr()_function_in_C++&apos;; string delim = &apos;_&apos;; // delimiter cout &lt;&lt; &apos; Your string with delimiter is: &apos; &lt;&lt; given_str &lt;&lt; endl; size_t pos = 0; string token1; // define a string variable // use find() function to get the position of the delimiters while (( pos = given_str.find (delim)) != std::string::npos) { token1 = given_str.substr(0, pos); // store the substring cout &lt;&lt; token1 &lt;&lt; endl; given_str.erase(0, pos + delim.length()); /* erase() function store the current positon and move to next token. */ } cout &lt;&lt; given_str &lt;&lt; endl; // it print last token of the string. } </pre> <p> <strong>Output</strong> </p> <pre> Your string with delimiter is: How_to_split_a_string_using_find()_and_substr()_function_in_C++ How to split a string using find() and substr() function in C++ </pre> <p>In the above program, we use a <strong>find()</strong> function inside the loop to repeatedly find the occurrence of the delimiter in the given string and then split it into tokens when the delimiter occurs. And the <strong>substr()</strong> function stores the sub-string to be printed. On the other hand, an erase() function stores the current position of the string and moves to the next token, and this process continues until we have got all the split strings.</p> <hr></=>

استخدم الدالة std::getline() لتقسيم السلسلة

وظيفة getline () هي وظيفة مكتبة قياسية في لغة C ++ تُستخدم لقراءة السلسلة من دفق الإدخال ووضعها في سلسلة المتجهات حتى يتم العثور على أحرف محددة. يمكننا ان نستخدم الأمراض المنقولة جنسيا::جيتلاين () تعمل عن طريق استيراد ملف الرأس.

العشاء مقابل العشاء

بناء الجملة

 getline(str, token, delim); 

لديها ثلاث معلمات:

شارع: Str هو متغير يخزن السلسلة الأصلية.

رمز: يقوم بتخزين رموز السلسلة المستخرجة من السلسلة الأصلية.

يشارك: وهو الحرف الذي يستخدم لتقسيم السلسلة. على سبيل المثال، الفاصلة (،)، المسافة ()، الشرطة (-)، إلخ.

برنامج لاستخدام وظيفة getline() لتقسيم السلاسل

لنفكر في مثال لتقسيم السلاسل باستخدام وظيفة getline() في لغة C++.

Program3.cpp

تحويل int إلى سلسلة
 #include #include #include #include using namespace std; int main() { string S, T; // declare string variables getline(cin, S); // use getline() function to read a line of string and store into S variable. stringstream X(S); // X is an object of stringstream that references the S string // use while loop to check the getline() function condition while (getline(X, T, &apos; &apos;)) { /* X represents to read the string from stringstream, T use for store the token string and, &apos; &apos; whitespace represents to split the string where whitespace is found. */ cout &lt;&lt; T &lt;&lt; endl; // print split string } return 0; } 

انتاج |

 Welcome to the JavaTpoint and Learn C++ Programming Language. Welcome to the JavaTpoint and Learn C++ Programming Language. 

برنامج لتقسيم السلسلة المعطاة باستخدام وظيفة getline()

لنفكر في مثال لتقسيم سلسلة معينة في لغة C++ باستخدام الدالة getline().

Program4.cpp

 #include #include #include #include void split_str( std::string const &amp;str, const char delim, std::vector &amp;out ) { // create a stream from the string std::stringstream s(str); std::string s2; while (std:: getline (s, s2, delim) ) { out.push_back(s2); // store the string in s2 } } int main() { std:: string s2 = &apos;Learn How to split a string in C++&apos;; const char delim = &apos; &apos;; /* define the delimiter like space (&apos; &apos;), comma (,), hyphen (-), etc. */ std::cout &lt;&lt; &apos;Your given string is: &apos; &lt;&lt; s2; std::vector out; // store the string in vector split_str (s2, delim, out); // call function to split the string // use range based for loop for (const auto &amp;s2: out) { std::cout &lt;&lt; &apos;
&apos; &lt;&lt; s2; } return 0; } 

انتاج |

 Your given string is: Learn How to split a string in C++ Learn How to split a string in C++ 

استخدم الدالة find() وsubstr() لتقسيم السلاسل

لنكتب برنامجًا لاستخدام الدالة find() والدالة substr() لتقسيم السلاسل المعطاة في لغة C++.

قائمة المصفوفات والقائمة المرتبطة

Program4.cpp

 #include #include using namespace std; int main() { // given string with delimiter string given_str = &apos;How_to_split_a_string_using_find()_and_substr()_function_in_C++&apos;; string delim = &apos;_&apos;; // delimiter cout &lt;&lt; &apos; Your string with delimiter is: &apos; &lt;&lt; given_str &lt;&lt; endl; size_t pos = 0; string token1; // define a string variable // use find() function to get the position of the delimiters while (( pos = given_str.find (delim)) != std::string::npos) { token1 = given_str.substr(0, pos); // store the substring cout &lt;&lt; token1 &lt;&lt; endl; given_str.erase(0, pos + delim.length()); /* erase() function store the current positon and move to next token. */ } cout &lt;&lt; given_str &lt;&lt; endl; // it print last token of the string. } 

انتاج |

 Your string with delimiter is: How_to_split_a_string_using_find()_and_substr()_function_in_C++ How to split a string using find() and substr() function in C++ 

في البرنامج أعلاه نستخدم يجد() وظيفة داخل الحلقة للعثور بشكل متكرر على حدوث المحدد في السلسلة المحددة ثم تقسيمه إلى رموز مميزة عند حدوث المحدد. و ال فرعية () تقوم الدالة بتخزين السلسلة الفرعية المراد طباعتها. من ناحية أخرى، تقوم وظيفة المسح () بتخزين الموضع الحالي للسلسلة وتنتقل إلى الرمز المميز التالي، وتستمر هذه العملية حتى نحصل على جميع السلاسل المقسمة.