字符串基础及常用算法

来源:互联网 发布:php视频网站源码下载 编辑:程序博客网 时间:2024/05/16 12:53

1.字符串的表示形式

    在C语言中,可以用两种方法访问一个字符串。

   (1).用字符数组存放一个字符串。

   (2).用字符指针指向一个字符串。


2.常用程序

    实现strcat(字符数组1,字符数组2)函数

    strcat函数是指连接两个字符数组中的字符串把字符串2接到字符串1的后面;

void strcat(char* pStr1,char* pStr2){if(NULL==pStr1 || NULL==pStr2)return;char* pTemp;pTemp=pStr1;while(*pTemp!='\0')pTemp++;while(*pStr2!='\0'){*pTemp++=*pStr2++;}*pTemp='\0';}

    实现字符串翻转函数Reverse(char *pBegin, char *pEnd)

///////////////////////////////////////////////////////////////////////// Reverse a string between two pointers// Input: pBegin - the begin pointer in a string//        pEnd   - the end pointer in a string///////////////////////////////////////////////////////////////////////void Reverse(char *pBegin, char *pEnd){      if(pBegin == NULL || pEnd == NULL)            return;      while(pBegin < pEnd)      {            char temp = *pBegin;            *pBegin = *pEnd;            *pEnd = temp;            pBegin ++, pEnd --;      }}

0 0