字符串有关操作函数

<pre name="code" class="html">// StringFunction.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"

int StrLen(const char *pStr);

char* StrCpy(char *pStrDest,const char *pStrSrc);

char* StrCat(char *pStr,const char *pStr1);

bool StrCmp(const char* pStr,const char* pStr1);

int main(int argc, char* argv[])
{
	char szBuf[256] = "hello";
    char szInput[256] = "word!";
	char szInputOne[256] ="hello";

    printf("szBuf = %s\n",szBuf);
    printf("szInput = %s\n",szInput);
	
	printf("%d\n",StrLen(szBuf));

    printf("copy szBuf =%s\n",StrCpy(szBuf,szInput));

	printf("szInput Link after szInputOne = %s\n",StrCat(szInputOne,szInput));

	printf("szBuf == szInputOne ? %d\n",StrCmp(szBuf,szInputOne));


	return 0;
}


/*求取字符串长度
====================================================================
//求取字符串长度方法一
  
int StrLen(const char *pStr)             //递归调用
{
   if( NULL == pStr || *pStr == '\0')
   {
      return 0;
   }
   else
   {
      return StrLen(pStr+1)+1;
   }
}

====================================================================
//求取字符串长度方法二

int StrLen(const char *pStr)
{ 
	if( NULL != pStr)
	{
		const char* pTemp = pStr;

       while(*pTemp++);

      return (pTemp - pStr -1);
	}
	return 0;
}

=======================================================================
//求取字符串长度方法三

int StrLen( const char *pStr)
{

   int nSize = 0;
 
    if( NULL != pStr)
	{
	  for(int i =0 ; '\0' != pStr[i];i++)
	  {
 	  		nSize++;
      }	  
	}
   return nSize;
}

========================================================================*/
//求取字符串长度方法四

int StrLen(const char *pStr)
{
   int nSize = 0;
  
	if( NULL != pStr)
	{
	  while(  '\0' != *pStr++)
	  {
	    nSize++; 
	  }	  		
    }
	
   return nSize;
}



/*拷贝字符串
=========================================================================
//拷贝字符串方法一

char* StrCpy(char *pStrDest, const char *pStrSrc)
{
	if( NULL != pStrDest && NULL != pStrSrc)
	{ 
		int nSize =  pStrSrc -  pStrDest;

        for(int i =0; '\0' != (pStrDest[i] = pStrDest[nSize++]);i++);
	}

     return pStrDest;
}

=========================================================================
拷贝字符串方法二

char* StrCpy(char *pStrDest,const char *pStrSrc)
{

   if( NULL != pStrDest && NULL != pStrSrc)
   {
     for(int i = 0; '\0' != (pStrDest[i] = pStrSrc[i]); i++);	 	   
   }
   
   return pStrDest;	  		
}

==========================================================================*/
拷贝字符串方法三

char* StrCpy(char *pStrDest,const char *pStrSrc)
{
    char *pTemp = pStrDest;

    if( NULL != pTemp && NULL != pStrSrc)
	{
	   while(*pTemp++ = *pStrSrc++);	  
	}

  return pStrDest;
	
}



/*字符串连接
==========================================================================
字符串连接方法一

char* StrCat(char *pStr,const char *pStr1)
{
	if( NULL != pStr && NULL != pStr1)
	{ 
		char *pTemp = pStr;

	    while(*pTemp++);
        pTemp--;
   
	    StrCpy(pTemp,pStr1);
	}

     return pStr;  
}

==========================================================================
字符串连接方法二

char* StrCat(char *pStr ,const char *pStr1)
{
  if( NULL != pStr && NULL != pStr1)
  {
      int nSize = StrLen(pStr);
      for(int i =0 ;pStr[nSize++] = pStr1[i];i++);	
  }

     return pStr;  
 }


字符串连接方法三
===========================================================================*/

char* StrCat(char *pStr ,const char *pStr1)
{
  if( NULL != pStr && NULL != pStr1)
  {    
      char *pTemp = pStr;
      while(*pTemp++);
	  pTemp--;
      while(*pTemp++ = *pStr1++);
  }

     return pStr;  
 }





/*字符串比较
========================================================================
//字符串比较方法一

bool StrCmp(const char* pStr,const char* pStr1)
{
   bool bFlag = false;
    
   if( NULL != pStr && NULL != pStr1)
   {   
	   int nSize = pStr1 - pStr;
        for(int i = 0; pStr[nSize++] == pStr[i] ;i++)
		{
		  if( pStr[i] == '\0')
		  {
		    return true;
		  }		   
		}
   }

   return bFlag;
}


========================================================================
//字符串比较方法二

bool StrCmp(const char* pStr,const char* pStr1)
{
   bool bFlag = false;
    
   if( NULL != pStr && NULL != pStr1)
   {   
        for(int i = 0; pStr[i] == pStr1[i] ;i++)
		{
		  if( pStr[i] == '\0')
		  {
		    return true;
		  }		   
		}
   }

   return bFlag;
}

========================================================================*/
//字符串比较方法三

bool StrCmp(const char* pStr,const char* pStr1)
{
   bool bFlag = false;
    
   if( NULL != pStr && NULL != pStr1)
   {   
       while( *pStr++ == *pStr1++)
	   { 
		   if( *pStr == '\0')
		   {
		     return true;
		   }		   
	   }
   }

   return bFlag;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值