【C语言】求一个字符串的长度(指针)

代码

使用指针来遍历字符串,直到遇到字符串结尾的空字符\0为止,统计字符数量即为字符串长度。 

#include<stdio.h>
#define n 20
int getlength(char *a) {
	int len = 0;
	while(*a!='\0'){
		len++;
		a++;
	}

	return len;
}
int main() {
	char *arr[n] = { 0 };
	int len;
	printf("please input the string:\n");
	scanf("%s", arr);
	len=getlength(arr);
	printf("the length of the string is:%d", len);
	return 0;
}

测试结果

猜你喜欢

转载自blog.csdn.net/qq_52442214/article/details/133559635