strcmp函数的重写

来源:互联网 发布:电动飞机杯 知乎 编辑:程序博客网 时间:2024/05/16 14:56

自己编程实现2个字符串的比较函数strcmp。

 

// strcmp函数重写.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<stdio.h>
#include<assert.h>

/*strcmp 函数算法实现*/
int strcmp(char *strSource,char *strDest)
 {
  assert(strSource != NULL && strDest != NULL);
  int flag = 0;
  while(*strSource  || *strDest )
  {
   if(*strSource > *strDest) 
   {
    flag = 1;
    break;
   }
   else if(*strSource == *strDest)
   {
    strSource++;
    strDest++;
    continue;
   }
   else
   {
    flag = -1;
    break;
   } 
  }
  return flag;
 }

int _tmain(int argc, _TCHAR* argv[])
{
 char strSource[20];
 char strDest[20];
 int flag = 0;
 scanf("%s",strSource);
 scanf("%s",strDest);
 flag = strcmp(strSource,strDest);
 if (flag > 0)
  {
   printf("strSource large than strDest.");
  }
 else if (flag == 0)
  {
   printf("strSource equal strDest."); 
  }
 else
  {
   printf("strSource small than strDest.");
  }
 return 0;
}

 

原创粉丝点击