Chinaunix首页 | 论坛 | 博客
  • 博客访问: 282507
  • 博文数量: 60
  • 博客积分: 2501
  • 博客等级: 少校
  • 技术积分: 774
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-16 13:27
文章分类

全部博文(60)

文章存档

2011年(1)

2010年(1)

2009年(58)

我的朋友

分类:

2009-08-14 16:41:26

(1)串:
计算机上非数值处理的对象基本上是字符串数据。串(string)是由有限个字符组成的序列,所以串又称为字符串(character string),一般记为:S=“a1a2a3a4a5...an”;其中s是串名,n是串中字符的个数,空串表示为s="";空格也是合法字符。(这个串咋都不包括结束符的,不管这个先接着看下去吧)
(2)串的运算:
求串的长度len(s);
判断串是否相等equal(s);
两个串的连接concat(s);
求某串的子串sub(s,start,ln,t)求子串的运算是已知某串和两个正整数start与ln,其运算结果t是在s从第start个字符开始,由连续ln个字符组成的子串(蛮绕的);
插入子串insert(s1,i,s2);
删除子串delete(s1,i,j)从第i个字符开始删除长度为j的子串;
置换replace(s,t,r)在已知串s中,以串r替换与串t相同的所有子串(word的替换功能???);
(3)串的存储结构:
串是种特殊的线性表,分为顺序存储和链式存储。
顺序存储结构:
串的最简单的顺序存储结构是采用非紧缩方式,每个存储单元存放一个字符(串不就这么一种存储方式吗???)。
紧缩格式的顺序存储格式,根据存储单元的容量给每个单元存入多个字符,最末的一个单元如果没有占满,可填充空格(有点明白了,非紧缩结构类似与结构体的字节对齐)。
计算机是以字节为存取单位的,字符的ACSII码正好是8位,看来都是紧缩的了。
链接存储结构:
每个存储接点有一个存放字符的域和一个存放指向下一个接点的指针域。这种方法空间利用率低。(没怎么听过哦,感觉没多大意思)。
(4)串的匹配运算:
就是判断某串是否是另一个已知串的子串(word的查找功能???)。
移植r1,r2要判断r2是否是r1的子串。
(5)字符串的处理函数
字符串有很多很多的处理,要一一列举也不容易,这里贴上一些程序源码:
函数1:判断一个字符数组是否是0,
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
//| Function:   IsNumeric()   
//| In:         char *chString   
//|                 In Char Or String Of Chars   
//| Return:     BOOL   
//| Notes:      This Function Checks To Is If A Char Or A String Of Chars Is Numeric Or Not   
//| Example:    char p[] = "5";   
//|             BOOL bRet = FALSE;   
//|             bRet = IsNumeric(p);    //Should Return True   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
  
BOOL IsNumeric(char *chString)  
{    
    char *buf;                     
    buf = chString;  
  
    //Loop Through The Pointer Using The Inline Function IsDigit   
    while (IsDigit(buf))  
    {  
        buf++;                      //Increase The Pointer   IsDigit函数用于判定一个ASCII字符是否介于‘0’——‘9’之间,。 
    }  
    return  (BOOL) (*buf==0x00);  
}     
  
/*##############################################################################################*/  
函数2:小写变大写函数  
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
//| Function:   Upcase()   
//| In:         char *chString   
//|                 In Char Or String Of Chars   
//| Return:     None   
//| Notes:      This Function Uppercases A Char Or Sting Of Chars   
//| Example:    char p[] = "t";   
//|             Upcase(p);          //Should Chang p To T   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/   
void Upcase(char *chString)  
{    
    string stTemp = "";                 
    char *buf;  
    unsigned int i =0;   
       
    buf = chString;  
  
    //Loop Through The Length Of The Pointer    
    while(*buf != 0x00)  
    {  
        //Make The Temp VAR Set To The Uppercase Of The Input VAR   
        stTemp += ToUpper(chString[i]);  
        buf++;  
        i++;  
    }  
  
    try  
    {  
        //Copy Back   
        strcpy(chString,stTemp.c_str());  
    }  
    catch(...)  
    {  
        return;  
    }  
}   
    
/*##############################################################################################*/   
 
 
函数3 大写变小写 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
//| Function:   Lowercase()   
//| In:         char *chString   
//|                 In Char Or String Of Chars   
//| Return:     None   
//| Notes:      This Function Lowercases A Char Or Sting Of Chars   
//| Example:    char p[] = "T";   
//|             Lowercase(p);           //Should Chang p To t   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
  
void Lowercase(char *chString)  
{  
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/  
    string stTemp = "";             //Temp String VAR   
    char *buf;  
    unsigned int i =0;  
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/  
  
    buf = chString;  
  
    //Loop Through The Length Of The Pointer    
    while(*buf != 0x00)  
    {  
        //Make The Temp VAR Set To The Lowercase Of The Input VAR   
        stTemp += ToLower(chString[i]);  
        buf++;  
        i++;  
    }  
  
    //Copy Back   
    strcpy(chString,stTemp.c_str());  
}   
  
/*##############################################################################################*/  
函数4 在字符串中去掉前后所有的空格的函数  
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
//| Function:   Trim()   
//| In:         char *t   
//|                 In Char Or String Of Chars   
//| Return:     None   
//| Notes:      This Function Trims Spaces From The Begining And The End Of A String   
//|             This Function Was Created By jim mcnamara on VB-World.net Forums (Modified Some)   
//| Example:    char p[] = "    TTTT    ";   
//|             Trim(p);            //Should Chang p To "TTTT"   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
  
void Trim(char *t)  
{  
    RTrim(t);  
    if (strlen(t))  
        LTrim(t);  
}   
   
/*##############################################################################################*/   
 
函数5 去掉后面的空格的函数 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
//| Function:   RTrim()   
//| In:         char *t   
//|                 In Char Or String Of Chars   
//| Return:     None   
//| Notes:      This Function Trims Spaces From The End Of A String   
//|             This Function Was Created By jim mcnamara on VB-World.net Forums (Modified Some)   
//| Example:    char p[] = "TTTT    ";   
//|             RTrim(p);           //Should Chang p To "TTTT"   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
  
void RTrim(char *t)  
{  
    char *buf;  
  
    if (!strlen(t)) return;  
  
    buf = t;  
    buf += strlen(t) -1;  
  
    while (*buf == ' ' && strlen(t) )  
        *buf-- = '\0';  
}   
   
/*##############################################################################################*/  
函数6 去掉前面的空格的函数  
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
//| Function:   LTrim()   
//| In:         char *t   
//|                 In Char Or String Of Chars   
//| Return:     None   
//| Notes:      This Function Trims Spaces From The Front Of A String   
//|             This Function Was Created By jim mcnamara on VB-World.net Forums (Modified Some)   
//| Example:    char p[] = "    TTTT";   
//|             LTrim(p);           //Should Chang p To "TTTT"   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
  
void LTrim(char *t)  
{  
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/  
    char tmp[128];              //Temp Char VAR   
    char *buf;                  //Pointer   
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/  
      
    if (!strlen(t)) return;  
  
    strcpy(tmp,t);  
    buf=tmp;  
  
    while (*buf == ' ' && strlen(buf))  *buf++;  
  
    strcpy(t,buf);  
}   
   
/*##############################################################################################*/   
 
函数7 返回字符串最后面的连续的len个字符  
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
//| Function:   *right()   
//| In:         const char *chString   
//|                 In Char Or String Of Chars   
//|             unsigned int len   
//|                 Length To Trim   
//| Return:     char*   
//| Notes:      This Function Trims Chars From Right To Left  <---   
//| Example:    char p[] = "TTTT1234";   
//|             LPCTSTR t;   
//|             t = right(p,3);     //Should Return "234"   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
  
char *right(const char *chString,unsigned int len)  
{  
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/  
    unsigned int i;             //Loop VAR   
    int k, nIndex;              //More Loop VARs   
    char *buf;                  //Temp String VAR   
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/  
  
    if (!strlen(chString))                      //If There Is No String   
        return NULL;                            //Return NULL   
  
    if (len > strlen(chString))                  //If The Amount To Copy Is Larger Than The String   
        len = strlen(chString);                 //Set The Length To The Length Of The String   
  
    buf = (char *)malloc(len + 1);  
  
    //Copy The Part We Want From The Orginal String To The Result   
    for (i = 0, nIndex = ((strlen(chString))-len), k = 0; i < len; i++)    
    {         
        if (chString[i] != '\0')            //If Not A NULL Char           
            buf[k++] = chString[nIndex++];  
        else  
            break;  
    }  
  
    buf[k] = '\0';                      //Add A NULL Char To The End Of The String   
  
    return buf;  
  
}   
   
/*##############################################################################################*/   
函数8 返回字符串最前面面的连续的len个字符   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
//| Function:   *left()   
//| In:         const char *stString   
//|                 In Char Or String Of Chars   
//|             unsigned int len   
//|                 Length To Trim   
//| Return:     char*   
//| Notes:      This Function Trims Chars From Left To Right  --->   
//| Example:    char p[] = "TTTT1234";   
//|             LPCTSTR t;   
//|             t = left(p,4);      //Should Return "TTTT"   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
  
char *left(const char *chString,unsigned int len)  
{  
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/  
    unsigned int i;             //Loop VAR   
    int k, nIndex;              //More Loop VARs   
    char *buf;                  //Temp String VARs   
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/  
  
    if (!strlen(chString))                      //If There Is No String   
        return NULL;                            //Return NULL   
  
    if (len > strlen(chString))                  //If The Amount To Copy Is Larger Than The String   
        len = strlen(chString);                 //Set The Length To The Length Of The String   
  
    buf = (char *)malloc(len + 1);  
  
    //Copy The Part We Want From The Orginal String To The Result   
    for (i = 0, nIndex = 0, k = 0; i < len; i++)   
    {         
        if (chString[nIndex] != '\0')           //If Not A NULL Char           
            buf[k++] = chString[nIndex++];  
        else  
            break;  
    }  
  
    buf[k] = '\0';                      //Add A NULL Char To The End Of The String   
  
    return buf;  
}   
  
/*##############################################################################################*/   

函数9 返回字符串中第start个字符后面的连续的len个字符   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
//| Function:   *mid()   
//| In:         const char *chString   
//|                 In Char Or String Of Chars   
//|             unsigned int start   
//|                 Starting Position   
//|             unsigned int len   
//|                 Length To Trim   
//| Return:     char*   
//| Notes:      This Function Trims Chars From A Location To Right  --->   
//| Example:    char p[] = "TTTT1234";   
//|             LPCTSTR t;   
//|             t = mid(p,3,5);     //Should Return "T1234"   
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/  
  
char *mid(const char *chString,unsigned int start,unsigned int len)  
{  
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/  
    int i, k, nActualEnd, nActualStart, nIndex;  
    char *result;  
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/  
      
    if(!strlen(chString))                   //If There Is No The Length    
        return NULL;                        //Return NULL   
  
    if (start > strlen(chString))            //Make Sure The Start Is Not More Than The Length   
        return NULL;                        //If It Is Return Null   
    if (start < 0)                           //If The Start Is Less Than 0   
        nActualStart = 0;                   //Set The nActualStart To 0   
    else          
        nActualStart = start;               //Else Set The nActualStart   
  
    if (len > strlen(chString))              //If The Length Is Longer Than The String   
        nActualEnd = strlen(chString);      //Set The nActualEnd To The Length Of The String   
    else          
        nActualEnd = len;                   //Else nActualEnd To The Length   
  
    //Make A String To Send Back   
    result = (char *)malloc(nActualEnd + 1);  
  
    //Copy The Part We Want From The Orginal String To The Result   
    for (i = 0, nIndex = nActualStart, k = 0; i < nActualEnd; i++)     
    {         
        if (chString[nIndex] != '\0')           //If Not A NULL Char           
            result[k++] = chString[nIndex++];  
        else  
            break;  
    }  
  
    result[k] = '\0';                       //Add A NULL Char To The End Of The String   
  
    return result;                          //Return The New String   
}   
  
/*##############################################################################################*/  
 
这些都是些很简单的字符串处理函数,还有很多很多的字符串处理函数
比如两个字符串的相加,相等,查找两个字符串中 所有相同字符子串中最长的子串,等等。可以上网去找哦。
阅读(1190) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~