编写一个求字符串长度的函数strlen(),再用strlen()函数编写一个函数reverse(s)的倒序递归程序,使字符串s逆序-简单

Posted bobo哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编写一个求字符串长度的函数strlen(),再用strlen()函数编写一个函数reverse(s)的倒序递归程序,使字符串s逆序-简单相关的知识,希望对你有一定的参考价值。

源程序:

 

#include < iostream >

#include < string >

using namespace std;

int strlen(char *str)

{

  int len = 0;

  while (str[len] != \'\\0\')

  {

    len++;

  }

  return len;

}

void revers(char *b)

{

  char c;

  int j, len;

  len = strlen(b);

  j = len / 2 - 1;

  while (j >= 0)

  {

    c = *(b + j);

    *(b + j) = *(b + len - j - 1);

    *(b + len - j - 1) = c;

    j--;

  }

  b[len] = \'\\0\';

}

void main()

{

  cin.get();

  char *s;

  char str[10];

  cout << "输入一个字符串:";

  s = str;

  cin.getline(s, 10);

  //char str[]={"1234567890"};

  cout << str << "----的长度:" << strlen(str) << endl;

  cout << str << endl;//倒序前

  revers(str);//

  cout << str << endl;//倒序后

  system("pause");

}

 运行结果:

以上是关于编写一个求字符串长度的函数strlen(),再用strlen()函数编写一个函数reverse(s)的倒序递归程序,使字符串s逆序-简单的主要内容,如果未能解决你的问题,请参考以下文章

strlen函数

strlen函数

C语言如何用strlen函数测定一个数组的长度 求程序

编写一个函数FindFirstSub,求三个字符串中的最长公共子串

c语言求数组长度strlen

sizeof()与strlen()