고정 직렬 조작

스 트 링 작업 은 데이터 구조 에서 도 매우 중요 한 부분 이다. 먼저 스 트 링, 스 트 링 길이, 스 트 링 이 같 고 빈 칸 스 트 링, 빈 스 트 링, 하위 스 트 링 의 개념 을 이해 해 야 한다.우 리 는 프로 그래 밍 과정 에서 문자열 에 대한 조작 은 문자열 구조의 사용 이다.
 :          , :“abcde”,“PI” 
  :        , :“abcde”   5,“PI”   2
   :              , :“abcde” “abcde”
   :             ,       ,      , :“   ”
  :       , :“”
  :        , :“abcde” “a”,“bc”,“bcd”     

헤더 파일:
/***************************************************************************************************** 
 *Copyright:Yue Workstation 
 * 
 *FileName:FixedLengthString.h 
 * 
 *Function:               
 * 
 *Author:Abel Lee 
 * 
 *CreateOn:2011-8-11 
 * 
 *Log:2011-8-11    
 *****************************************************************************************************/ 

#ifndef FIXED_LENGTH_STRING_H 
#define FIXED_LENGTH_STRING_H 

#include  
#include  
#include  
#include "global.h" 

typedef unsigned char SString[MAX_LENGTH + 1];//SString[0]      

SString *StringCopy(SString *S, SString T); 
SString *StringClear(SString *S); 
SString *StringCat(SString *S, const SString T); 
SString *StringReplace(SString *S, SString T, SString V); 
int StringInstert(SString *S, SString T, int pos); 
int StringInit(SString *S, const char *str); 
int StringCompare(SString S,SString T); 
int StringIndex(SString S, SString T, int pos); 
int StringLength(SString S); 
int StringDestroy(SString *S); 
void StringPrint(SString S); 
int StringSub(SString *S, SString T, int pos, int len); 

#endif

원본 파일:
/***************************************************************************************************** 
 *Copyright:Yue Workstation 
 * 
 *FileName:FixedLengthString.c 
 * 
 *Function:       
 * 
 *Author:Abel Lee 
 * 
 *CreateOn:2011-8-11 
 * 
 *Log:2011-8-11    
 *****************************************************************************************************/ 

#include "../inc/FixedLengthString.h" 

/**************************************************************************************************** 
 *Function Name:StringCopy 
 * 
 *Function:    
 * 
 *Parameter:     S:       
 *             str:       
 * 
 *Return Value:    0,    -1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-9-15 
 ***************************************************************************************************/ 
SString *StringCopy(SString *S, SString T) 
{ 
    int i = 0; 

    if(*S == NULL || T == NULL) 
    { 
        return NULL; 
    } 

    while(T[i]) 
    { 
        (*S)[i] = T[i]; 
        i++; 
    } 

    (*S)[i] = T[i]; 
    return S; 
} 

/**************************************************************************************************** 
 *Function Name: StringCompare 
 * 
 *Function:    S T     
 * 
 *Parameter:     S:         
 *               T:         
 * 
 *Return Value:     0,S > T   1,S < T   -1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-9-16 
 ***************************************************************************************************/ 
 int StringCompare(SString S,SString T) 
{ 
     int i = 0; 
     int flag = 0; 

     if(S[0] - T[0] >= 0) 
     { 
         flag = S[0]; 
     } 
     else 
     { 
         flag = T[0]; 
     } 
     for(i = 1; i < flag; i++) 
     { 
         if(S[i] - T[i] > 0) 
         { 
             return 1; 
         } 
         else if(S[i] - T[i] < 0) 
         { 
             return -1; 
         } 
         else 
         { 

         } 
     } 
     return 0; 
} 

 /**************************************************************************************************** 
  *Function Name: StringClear 
  * 
  *Function:       
  * 
  *Parameter:     S:        
  * 
  *Return Value:     S,    NULL 
  * 
  *Author:Abel Lee 
  * 
  *Log:2011-9-16 
  ***************************************************************************************************/ 
SString *StringClear(SString *S) 
{ 
    int i = 0; 

    if(S == NULL) 
    { 
        return NULL; 
    } 

    while((*S)[i]) 
    { 
        (*S)[i] = '\0'; 
        i++; 
    } 
    return S; 
} 

/**************************************************************************************************** 
 *Function Name: StringCat 
 * 
 *Function:      , T   S   
 * 
 *Parameter:     S:    
 *               T:     
 * 
 *Return Value:     S,    NULL 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-9-16 
 ***************************************************************************************************/ 
SString *StringCat(SString *S, const SString T) 
{ 
    int i = 1; 
    int j = 1; 

    if(S == NULL || T == NULL) 
    { 
        return NULL; 
    } 

    if((*S)[0] + T[0] > MAX_LENGTH) 
    { 
        return NULL; 
    } 

    while((*S)[i]) 
    { 
        i++; 
    } 
    while(T[j]) 
    { 
        (*S)[i] = T[j]; 
        i++; 
        j++; 
    } 
    (*S)[i] = '\0'; 
    (*S)[0] += T[0]; 
    return S; 
} 

/**************************************************************************************************** 
 *Function Name: StringSub 
 * 
 *Function:     T  pos        len      S  
 * 
 *Parameter:     S:     
 *               T:   
 *             pos:       
 *             len:       
 * 
 *Return Value:     0,    -1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-9-20 
 ***************************************************************************************************/ 
int StringSub(SString *S, SString T, int pos, int len) 
{ 
    int i = 1; 

    if(pos < 1 || pos > T[0] || len < 0 || len > T[0] - pos + 1) 
    { 
        perror("Input Parameter is error!  pos < 1 || pos > T[0] || len < 0 || len > T[0] - pos + 1
"); return -1; } (*S)[0] = len; while(len--) { (*S)[i++] = T[pos++]; } (*S)[i] = '\0'; return 0; } /**************************************************************************************************** *Function Name: StringReplace * *Function: V S T * *Parameter: S: * T: S * V: S T * *Return Value: , -1 * *Author:Abel Lee * *Log:2011-9-16 ***************************************************************************************************/ SString *StringReplace(SString *S, SString T, SString V) { int pos = 1; int len = T[0]; int i = 0; SString STemp; SString StrLeft; if(len <= 0 || (*S)[0] < T[0]) { perror("Input Parameter is error! len <= 0 || (*S)[0] < T[0]
"); return NULL; } while((*S)[0] - pos +1 >= len) { StringSub(&STemp,*S,pos,len); if(StringCompare(STemp,T) == 0) { i = 0; while((*S)[i+pos+len-1]) { i++; StrLeft[i] = (*S)[i+pos+len-1]; } StrLeft[i] = '\0'; StrLeft[0] = i - 1; (*S)[pos] = '\0'; (*S)[0] = pos -1; StringCat(S,V); StringCat(S,StrLeft); pos = pos + V[0]; continue; } pos++; } return S; } /**************************************************************************************************** *Function Name: StringInstert * *Function: S pos T * *Parameter: S: * T: * pos: * *Return Value: 0, -1 * *Author:Abel Lee * *Log:2011-9-21 ***************************************************************************************************/ int StringInstert(SString *S, SString T, int pos) { SString StrLeft; int i = 1; if((*S)[0] + T[0] > MAX_LENGTH || pos < 1 || pos > (*S)[0] + 1) { perror("Input parameter is error,(*S)[0] + T[0] > MAX_LENGTH || pos < 1 || pos > (*S)[0] + 1
"); return -1; } while((*S)[i+pos-1]) { StrLeft[i] = (*S)[i+pos-1]; i++; } StrLeft[i] = '\0'; StrLeft[0] = i - 1; (*S)[pos] = '\0'; (*S)[0] = pos -1; StringCat(S,T); StringCat(S,StrLeft); return 0; } /**************************************************************************************************** *Function Name: StringIndex * *Function: , S pos T, S * *Parameter: S: * T: * pos: * *Return Value: , 0, -1 * *Author:Abel Lee * *Log:2011-9-16 ***************************************************************************************************/ int StringIndex(SString S, SString T, int pos) { int len = T[0]; SString STemp; if(pos < 1 || S[0] < T[0] || pos > S[0]) { perror("Input Parameter is error! pos < 1 || S[0] < T[0] || pos > S[0]
"); return -1; } while(S[0] - pos +1 >= len) { StringSub(&STemp,S,pos,len); if(StringCompare(STemp,T) == 0) { return pos; } pos++; } return 0; } /**************************************************************************************************** *Function Name:StringInit * *Function: * *Parameter: S: * str: * *Return Value: 0, -1 * *Author:Abel Lee * *Log:2011-9-15 ***************************************************************************************************/ int StringInit(SString *S, const char *str) { int i = 0; const char *p = str; if(strlen(str) > MAX_LENGTH) { perror("The string length is very large!
"); return -1; } while(*p != '\0') { i++; (*S)[i] = *p; p++; } (*S)[0] = i; (*S)[++i] = '\0'; return 0; } /**************************************************************************************************** *Function Name:StringInit * *Function: * *Parameter: S: * *Return Value: 0, -1 * *Author:Abel Lee * *Log:2011-9-15 ***************************************************************************************************/ void StringPrint(SString S) { printf("%s
",(char *)(S+1)); return; } /**************************************************************************************************** *Function Name: StringLength * *Function: * *Parameter: S: * *Return Value: * *Author:Abel Lee * *Log:2011-9-16 ***************************************************************************************************/ int StringLength(SString S) { return S[0]; } /**************************************************************************************************** *Function Name: StringDestroy * *Function: * *Parameter: S: * *Return Value: 0, -1 * *Author:Abel Lee * *Log:2011-9-16 ***************************************************************************************************/ int StringDestroy(SString *S) { int i = 0; if(S == NULL) { return -1; } while((*S)[i]) { (*S)[i] = '\0'; i++; } return 0; }

좋은 웹페이지 즐겨찾기