[C++基礎]032_常用的字串處理函數(strcat,strcpy,strcmp,strupr,strlwr,strlen)

來源:互聯網
上載者:User

1. strcat——字串串連

 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 }

※注意點,第一個字串數組要足夠大,否則會有越界問題。

2. strcpy——字串拷貝

 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 }

※注意點,第一個字串數組要足夠大,否則會有越界問題。另外第二個參數可以不是數組,可以是字元。

3. strcmp——字串比較函數

 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 }

※注意點,前者大,返回1;後者大,返回-1;相等,返回0。

4. strupr——小寫轉大寫

 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——大寫轉小寫

 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——擷取字串長度

 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 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.