C语言的故事:自己没有想象中那么狠

     今天准备开始看minigui的代码了,事先想把C语言复习一遍,原来自我感觉非常良好的C语言,今天看了一天才发现自己基础是如此的差,唉!!!!看来还需努力啊!!!把今天的体会记录一下也好!
     写了四个字符串操作的函数,不用库函数写的,不怎么好,等待以后再改进吧!!
      求字符串长度函数:
      long StrLength(const char* srcStr)
      {
           _ASSERT(srcStr != NULL);
           long len = 0;
           while (*(srcStr+(len++)) != '\0')
                  ;
           return len-1;
       }
     
       字符串拷贝函数:
       char* StrCopy(char* destStr, const char* srcStr)
        {
             _ASSERT(destStr != NULL && srcStr != NULL);
             char* tmp = destStr;
             while ((*destStr++ = *srcStr++) != '\0')
                      ;
             return tmp;
         }

       字符串比较函数:
        int StrCmp(const char* srcStr, const char* cmpStr)
        {
             _ASSERT(cmpStr != NULL && srcStr != NULL);
             enum emSta = { LOWER = -1, EQUAL = 0; LARGER = 1;};
             int sta = EQUAL;
 
             while (*srcStr++ != '\0' || *cmpStr++ != '\0')
             {
                      if (*srcStr != *cmpStr)
                      {
                               sta = (*srcStr > *cmpStr) ? LARGER :  LOWER;
                                       break;
                      }
             }
        
            if (EQUAL == sta)
            {
                     if ( *srcStr != '\0')
                              sta = LARGER;
                     else if (*cmpStr != '\0')
                              sta = LOWER;
            }
        
            return sta;
        }

        查找子串函数:
        long SubStrPos(const char* srcStr, const char* subStr)
        {
                 _ASSERT(subStr != NULL && srcStr != NULL);
                 long sLen = StrLength(srcStr);
                 long subLen = StrLength(subStr);
                 int pos = 0;
 
                 if (subLen > sLen || 0 == subLen)
                         return -1;
 
                 while (pos+subLen <= sLen)
                 {
                          if (srcStr[pos] == subStr[0])
                          {
                                   int i;
                                   for (i=1; i<subLen; i++)
                                   {
                                            if (srcStr[pos+i] != subStr[i])
                                                    break;
                                   }
   
                                   if (i == subLen)
                                          return pos;
                          }
                          ++pos;
                 }
 
                 return -1;
        }
明天在看看数据结构的知识,现在什么算法都忘光了,这还是程序员吗?@_@

你可能感兴趣的:(C语言)