【1】字符串操作
#include
#include
#include
int main(int argc, char *argv[])
{
char a[] = "abc";
//char b[] = {"5544"};
char b[] = {'d', 'e', 'f'};
printf("a slen=%d,b slen=%d\n", strlen(a),strlen(b));
printf("a = %s, b = %s\n", a, b);
printf("asize len = %d, bsize len = %d\n", sizeof(a),sizeof(b));
return 0;
}
//注:栈分配原则:从高地址->低地址分配;
//运行结果
/*
a slen=3,b slen=7
a = abc, b = def0abc
asize len = 4, bsize len = 4
*/
【2】调换一个字符串的首尾
#include
#include
int swap(char *str)
{
int length = strlen(str);
char * p1 = str;
char * p2 = str + length - 1;
while(p1 < p2){
/*
char c = *p1;
*p1 = *p2;
*p2 = c;
*/
*p1^=*p2; //I = I ^ j
*p2^=*p1; //j = j ^ i
*p1^=*p2; // I = I ^ j
++p1;
--p2;
}
return 0;
}
int main()
{
char str[] = "ABCD1234efgh";
printf("str is %s\n",str);
swap(str);
printf("str now is %s\n",str);
}
【3】
#include
#include
int main()
{
char *a ="hello";
char *b ="hello";
if(a==b)
printf("YES");
else
printf("NO");
}
//这个问题比较有趣
//是一个常量字符串。位于静态存储区,它在程序生命期内恒定不变。
//如果编译器优化的话,会有可能a和b同时指向同一个hello的。则地址相同。
//如果编译器没有优化,那么就是两个不同的地址,则不同
阅读(847) | 评论(0) | 转发(0) |