【C-串比较的错误方式】不能直接比较
char buffer1[6]="hello"; char buffer2[6]="hello"; cout<<(buffer1==buffer2?"":"not")<<"equal\n";
【C-串的各种操作】
#include<iostream> #include<string.h>//C-串操作的头文件 using namespace std; int main() { char* s1="hello "; char* s2="123"; char a[20]; strcpy(a,s1); //复制 cout<<(strcmp(a,s1)==0?"":" not")<<"equal\n"; //比较 cout<<strcat(a,s2)<<endl; //连接 cout<<strrev(a)<<endl; //倒置 cout<<strset(a,‘c‘)<<endl; //设置 cout<<(strstr(s1,"ell")?"":"not ")<<"found\n"; //查找串 cout<<(strchr(s1,‘c‘)?"":"not ")<<"found\n"; //查找字符 return 0; }
C-串知识整理
时间: 2024-10-11 08:11:58