LENGTH( len, str, p, n ) // 一个字符串的长度//
- input(str)
- len<-length(str)
- output(len)
length(p) //计算字符串长度//
- n<-0
- while(data(p!=’\0’) do //统计字符串中字符个数//
- n<-n+1;p<-p+1
- end(while)
- return n
测试用例:asdafasf
测试结果:the string has 8 characters.
源程序:
#include"iostream"
using namespace std;
int length(char *p)
{
int n=0;
while(*p!='\0')
{
n++;
p++;
}
return n;
}
int main()
{
int len;
char str[20];
printf("please input a string:\n");
scanf("%s",str);
len=length(str);
printf("the string has %d characters.",len);
system("pause");
return 0;
}