[C ++ Basics] 032_common string processing functions (strcat, strcpy, strcmp, strupr, strlwr, strlen)

Source: Internet
Author: User

1. strcat -- string connection

 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "abcd"; 7     char str1[] = "abc"; 8     strcat(str, str1); 9     cout<<str<<endl;10 11     system("pause");12     return 0;13 }

※Note: the first string array must be large enough; otherwise, an out-of-bounds problem may occur.

2. strcpy -- Copy strings

 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "abcd"; 7     char str1[] = "abc"; 8     strcpy(str, str1); 9     cout<<str<<endl;10 11     system("pause");12     return 0;13 }

※Note: the first string array must be large enough; otherwise, an out-of-bounds problem may occur. The second parameter can be an array or a character.

3. strcmp -- string comparison Function

 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "abcd"; 7     char str1[] = "abc"; 8     if(0 == strcmp(str, str1)){ 9         cout<<"Equal."<<endl;10     }else{11         cout<<"Unequal."<<endl;12     }13 14     system("pause");15     return 0;16 }

※Note: if the former is large, 1 is returned. If the latter is large,-1 is returned. If the former is equal, 0 is returned.

4. strupr -- lowercase to uppercase

 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "abcdf"; 7     char str1[] = "abcde"; 8     strupr(str); 9     cout<<str<<endl;10 11     system("pause");12     return 0;13 }

5. strlwr -- convert uppercase to lowercase

 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "AASdf"; 7     strlwr(str); 8     cout<<str<<endl; 9 10     system("pause");11     return 0;12 }

 

6. strlen -- get the string length

 1 #include <iostream> 2 using namespace std; 3  4 int main(){ 5  6     char str[15] = "AASdf"; 7     cout<<strlen(str)<<endl; 8  9     system("pause");10     return 0;11 }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.