计算字符串长度
#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);
}
本文介绍了使用C语言进行字符串长度计算及字符串合并的基本方法。通过两个示例,详细展示了如何利用指针遍历字符串来获取其长度,以及如何将两个字符串连接在一起。适合初学者理解和实践C语言中的字符串操作。
3908

被折叠的 条评论
为什么被折叠?



