计算字符串长度
#include <stdio.h>
int main()
{
char s1[100]=“hello”;
char s2[100]=“world”;
int len=0;//计数
char *p1=s1;
while(*p1)//*p1的内容为0的时候,表示字符串结束了
{
p1++;
len++:
}
printf("字符串的长度为%d \n",len);
return 0;
}
合并字符串
#include <stdio.h>
int main()
{
char s1[100]=“hello”;
char s2[100]=“world”;
char *p1=s1;
while(*p1)
{
p1++;
}
char *p2=s2;
while(*p2)
{
*p1=*p2;//从s1的最后开始,从s2的首元素开始
p1++;
p2++;//这三行也可以写成*p1++=*p2++;
}
printf("s1=%s\n",s1);
}