[C++基础]032_常用的字符串处理函数(strcat,strcpy,strcmp,strupr,strlwr,strlen)...
生活随笔
收集整理的這篇文章主要介紹了
[C++基础]032_常用的字符串处理函数(strcat,strcpy,strcmp,strupr,strlwr,strlen)...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 }轉載于:https://www.cnblogs.com/alephsoul-alephsoul/archive/2012/10/17/2727040.html
總結
以上是生活随笔為你收集整理的[C++基础]032_常用的字符串处理函数(strcat,strcpy,strcmp,strupr,strlwr,strlen)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 戴尔服务器R740-iDRAC管理卡远程
- 下一篇: 给MVC中的V设计一个框架