C++,设计一个函数sLength 求字符串的长度,其中函数的参数为指向待求长度的字符串的指针,

C++,设计一个函数sLength 求字符串的长度,其中函数的参数为指向待求长度的字符串的指针,在主函数中输入一个字符串,调用sLength()函数计算该字符串的长度并输出

第1个回答  2018-03-20
#include <iostream>
using namespace std;

int sLength(char *s) {
  int len = 0;
  while(*s++!=0) len++;
  return len;
}

int main() {
  //char *s = "This is a test string";
  char s[200];
  cin >> s;
  cout << "sLength(s)=" << sLength(s) << endl;
  return 0;
}

本回答被提问者和网友采纳
相似回答