strlen()求字符串长度

求字符串长度

#include "stdafx.h"
#include<iostream>
using namespace std;
#define ERROR 1
int my_strlen(const char *p)
{

	if (NULL == p)return ERROR;
	const char *str = p;
	for (; *p != '\0'; p++){}
	return p - str;
	//int num = 0;
	//if (NULL == p)return ERROR;
	//for (; p[num]; ++num);
	//return num;
}
int main()
{
	cout << "请输入一个字符串:";
	int len;
	char ch1[20] ;
	cin >> ch1;
	len = my_strlen(ch1);
	switch (len)
	{
	case ERROR:cout << "输入字符串为NULL" << endl; break;
	default:cout << "字符串长度为:" << len << endl; break;
	}
	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43886592/article/details/85252353