logo

C++ 문자열

C++에서 문자열은 다음의 객체입니다. 표준::문자열 문자 시퀀스를 나타내는 클래스입니다. 연결, 비교, 변환 등과 같은 문자열에 대한 많은 작업을 수행할 수 있습니다.


C++ 문자열 예

C++ 문자열의 간단한 예를 살펴보겠습니다.

 #include using namespace std; int main( ) { string s1 = &apos;Hello&apos;; char ch[] = { &apos;C&apos;, &apos;+&apos;, &apos;+&apos;}; string s2 = string(ch); cout&lt;<s1<<endl; cout<<s2<<endl; } < pre> <p>Output:</p> <pre> Hello C++ </pre> <hr> <h2>C++ String Compare Example</h2> <p>Let&apos;s see the simple example of string comparison using strcmp() function.</p> <pre> #include #include using namespace std; int main () { char key[] = &apos;mango&apos;; char buffer[50]; do { cout&lt;&gt;buffer; } while (strcmp (key,buffer) != 0); cout&lt;<'answer is correct!!'<<endl; return 0; } < pre> <p>Output:</p> <pre> What is my favourite fruit? apple What is my favourite fruit? banana What is my favourite fruit? mango Answer is correct!! </pre> <hr> <h2>C++ String Concat Example</h2> <p>Let&apos;s see the simple example of string concatenation using strcat() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); cout &lt;&lt; &apos;Enter the buffer string: &apos;; cin.getline(buffer, 25); strcat(key, buffer); cout &lt;&lt; &apos;Key = &apos; &lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos; &lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: Welcome to Enter the buffer string: C++ Programming. Key = Welcome to C++ Programming. Buffer = C++ Programming. </pre> <hr> <h2>C++ String Copy Example</h2> <p>Let&apos;s see the simple example of copy the string using strcpy() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); strcpy(buffer, key); cout &lt;&lt; &apos;Key = &apos;&lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos;&lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let&apos;s see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = &apos;Welcome to C++ Programming&apos;; cout &lt;&lt; &apos;Length of String = &apos; &lt;&lt; strlen(ary)&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string&amp; str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string&amp; str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string&amp; replace(int pos,int len,string&amp; str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string&amp; append(const string&amp; str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char&amp; at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string&amp; str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string&amp; str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string&amp; str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string&amp; str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string&amp; str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string&amp; insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string&amp; assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string&amp; str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char&amp; back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string&amp; erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char&amp; front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&amp;&#xFFFD; operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string&amp; operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;></pre></endl;></pre></'answer></pre></s1<<endl;>

C++ 문자열 비교 예

strcmp() 함수를 사용하여 문자열 비교의 간단한 예를 살펴보겠습니다.

 #include #include using namespace std; int main () { char key[] = &apos;mango&apos;; char buffer[50]; do { cout&lt;&gt;buffer; } while (strcmp (key,buffer) != 0); cout&lt;<\'answer is correct!!\'<<endl; return 0; } < pre> <p>Output:</p> <pre> What is my favourite fruit? apple What is my favourite fruit? banana What is my favourite fruit? mango Answer is correct!! </pre> <hr> <h2>C++ String Concat Example</h2> <p>Let&apos;s see the simple example of string concatenation using strcat() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); cout &lt;&lt; &apos;Enter the buffer string: &apos;; cin.getline(buffer, 25); strcat(key, buffer); cout &lt;&lt; &apos;Key = &apos; &lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos; &lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: Welcome to Enter the buffer string: C++ Programming. Key = Welcome to C++ Programming. Buffer = C++ Programming. </pre> <hr> <h2>C++ String Copy Example</h2> <p>Let&apos;s see the simple example of copy the string using strcpy() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); strcpy(buffer, key); cout &lt;&lt; &apos;Key = &apos;&lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos;&lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let&apos;s see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = &apos;Welcome to C++ Programming&apos;; cout &lt;&lt; &apos;Length of String = &apos; &lt;&lt; strlen(ary)&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string&amp; str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string&amp; str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string&amp; replace(int pos,int len,string&amp; str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string&amp; append(const string&amp; str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char&amp; at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string&amp; str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string&amp; str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string&amp; str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string&amp; str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string&amp; str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string&amp; insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string&amp; assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string&amp; str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char&amp; back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string&amp; erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char&amp; front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&amp;&#xFFFD; operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string&amp; operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;></pre></endl;></pre></\'answer>

C++ 문자열 연결 예

strcat() 함수를 사용하여 문자열 연결의 간단한 예를 살펴보겠습니다.

사전순
 #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); cout &lt;&lt; &apos;Enter the buffer string: &apos;; cin.getline(buffer, 25); strcat(key, buffer); cout &lt;&lt; &apos;Key = &apos; &lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos; &lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: Welcome to Enter the buffer string: C++ Programming. Key = Welcome to C++ Programming. Buffer = C++ Programming. </pre> <hr> <h2>C++ String Copy Example</h2> <p>Let&apos;s see the simple example of copy the string using strcpy() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); strcpy(buffer, key); cout &lt;&lt; &apos;Key = &apos;&lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos;&lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let&apos;s see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = &apos;Welcome to C++ Programming&apos;; cout &lt;&lt; &apos;Length of String = &apos; &lt;&lt; strlen(ary)&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string&amp; str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string&amp; str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string&amp; replace(int pos,int len,string&amp; str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string&amp; append(const string&amp; str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char&amp; at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string&amp; str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string&amp; str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string&amp; str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string&amp; str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string&amp; str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string&amp; insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string&amp; assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string&amp; str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char&amp; back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string&amp; erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char&amp; front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&amp;&#xFFFD; operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string&amp; operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;></pre></endl;>

C++ 문자열 복사 예

strcpy() 함수를 사용하여 문자열을 복사하는 간단한 예를 살펴보겠습니다.

 #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); strcpy(buffer, key); cout &lt;&lt; &apos;Key = &apos;&lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos;&lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let&apos;s see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = &apos;Welcome to C++ Programming&apos;; cout &lt;&lt; &apos;Length of String = &apos; &lt;&lt; strlen(ary)&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string&amp; str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string&amp; str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string&amp; replace(int pos,int len,string&amp; str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string&amp; append(const string&amp; str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char&amp; at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string&amp; str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string&amp; str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string&amp; str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string&amp; str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string&amp; str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string&amp; insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string&amp; assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string&amp; str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char&amp; back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string&amp; erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char&amp; front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&amp;&#xFFFD; operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string&amp; operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;>

C++ 문자열 길이 예

strlen() 함수를 사용하여 문자열 길이를 찾는 간단한 예를 살펴보겠습니다.

 #include #include using namespace std; int main() { char ary[] = &apos;Welcome to C++ Programming&apos;; cout &lt;&lt; &apos;Length of String = &apos; &lt;&lt; strlen(ary)&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string&amp; str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string&amp; str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string&amp; replace(int pos,int len,string&amp; str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string&amp; append(const string&amp; str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char&amp; at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string&amp; str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string&amp; str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string&amp; str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string&amp; str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string&amp; str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string&amp; insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string&amp; assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string&amp; str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char&amp; back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string&amp; erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char&amp; front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&amp;&#xFFFD; operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string&amp; operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;>

C++ 문자열 함수

기능 설명
int 비교(const string& str) 두 개의 문자열 개체를 비교하는 데 사용됩니다.
정수 길이() 문자열의 길이를 구하는데 사용됩니다.
무효 교환(문자열&문자열) 두 문자열 개체의 값을 바꾸는 데 사용됩니다.
문자열 하위 문자열(int pos,int n) n개의 문자로 구성된 새로운 문자열 객체를 생성합니다.
정수 크기() 문자열의 길이를 바이트 단위로 반환합니다.
크기 조정 무효(int n) 문자열의 길이를 최대 n자까지 조정하는 데 사용됩니다.
문자열& 바꾸기(int pos,int len,string& str) 문자 위치 pos에서 시작하고 len 문자에 걸쳐 있는 문자열 부분을 대체합니다.
문자열& 추가(const 문자열& str) 다른 문자열 개체의 끝에 새 문자를 추가합니다.
char& at(int pos) 지정된 위치 pos에서 개별 문자에 액세스하는 데 사용됩니다.
int find(문자열& str,int pos,int n) 매개변수에 지정된 문자열을 찾는 데 사용됩니다.
int find_first_of(string& str,int pos,int n) 지정된 시퀀스의 첫 번째 발생을 찾는 데 사용됩니다.
int find_first_not_of(string& str,int pos,int n ) 문자열에 지정된 문자와 일치하지 않는 첫 번째 문자를 문자열에서 검색하는 데 사용됩니다.
int find_last_of(string& str,int pos,int n) 문자열에서 지정된 시퀀스의 마지막 문자를 검색하는 데 사용됩니다.
int find_last_not_of(string& str,int pos) 지정된 순서와 일치하지 않는 마지막 문자를 검색합니다.
문자열&삽입() 위치 pos가 나타내는 문자 앞에 새 문자를 삽입합니다.
정수 최대_크기() 문자열의 최대 길이를 찾습니다.
push_back 무효(문자 ch) 문자열 끝에 새 문자 ch를 추가합니다.
무효 pop_back() 문자열의 마지막 문자를 제거합니다.
문자열&할당() 문자열에 새로운 값을 할당합니다.
정수 복사(문자열&문자열) 문자열의 내용을 다른 문자열로 복사합니다.
문자&뒤로() 마지막 문자의 참조를 반환합니다.
반복자 시작() 첫 번째 문자의 참조를 반환합니다.
정수 용량() 문자열에 할당된 공간을 반환합니다.
const_iterator cbegin() 문자열의 첫 번째 요소를 가리킵니다.
const_iterator cend() 문자열의 마지막 요소를 가리킵니다.
무효 클리어() 문자열에서 모든 요소를 ​​제거합니다.
const_reverse_iterator crbegin() 문자열의 마지막 문자를 가리킵니다.
const_char* 데이터() 문자열의 문자를 배열로 복사합니다.
부울 비어 있음() 문자열이 비어 있는지 여부를 확인합니다.
문자열&지우기() 지정된 대로 문자를 제거합니다.
문자&앞() 첫 번째 문자의 참조를 반환합니다.
문자열&� 연산자+=() 문자열 끝에 새 문자를 추가합니다.
문자열& 연산자=() 문자열에 새 값을 할당합니다.
문자 연산자[](pos) 지정된 위치 pos에서 문자를 검색합니다.
정수 찾기() 문자열의 마지막 항목을 검색합니다.
반복자 끝() 문자열의 마지막 문자를 참조합니다.
reverse_iterator 렌드() 문자열의 첫 번째 문자를 가리킵니다.
Shrink_to_fit() 무효 용량을 줄여서 스트링 크기와 동일하게 만들어줍니다.
문자* c_str() null로 끝나는 문자 시퀀스를 포함하는 배열에 대한 포인터를 반환합니다.
const_reverse_iterator crend() 문자열의 첫 번째 문자를 참조합니다.
reverse_iterator rbegin() 문자열의 마지막 문자를 참조합니다.
무효 예약(inr len) 용량변경을 요청합니다.
allocator_type get_allocator(); 문자열과 연관된 할당된 개체를 반환합니다.