# include <iostream> # define MAX_SIZE 15
# define OK 1
# define ERROR 0
# define TRUE 1
# define FALSE 0
typedef int Status;
typedef char ElemType; typedef ElemType String[ MAX_SIZE + 1 ] ;
Status StrAssign ( String T, const char * chars)
{ int length = strlen ( chars) ; if ( length > MAX_SIZE) return ERROR; T[ 0 ] = ( char ) length; for ( int i = 1 ; i <= length; i++ ) { T[ i] = chars[ i - 1 ] ; } return OK;
}
Status StrCopy ( String T, String S)
{ for ( int i = 0 ; i <= S[ 0 ] ; i++ ) { T[ i] = S[ i] ; } return OK;
}
Status StrEmpty ( String T)
{ return T[ 0 ] == 0 ? TRUE : FALSE;
}
int StrCompare ( String T, String S)
{ for ( int i = 1 ; ( i <= T[ 0 ] ) && ( i <= T[ 0 ] ) ; i++ ) { if ( T[ i] > S[ i] ) return 1 ; else if ( T[ i] < S[ i] ) return - 1 ; } if ( T[ 0 ] > S[ 0 ] ) return 1 ; else if ( T[ 0 ] < S[ 0 ] ) return - 1 ; else if ( T[ 0 ] == S[ 0 ] ) return 0 ;
}
Status StrLength ( String T)
{ return ( int ) T[ 0 ] ;
}
Status ClearString ( String T)
{ T[ 0 ] = 0 ; return OK;
}
Status Concat ( String T, String S1, String S2)
{ if ( ( ( int ) S1[ 0 ] + ( int ) S2[ 0 ] ) <= MAX_SIZE) { T[ 0 ] = ( char ) ( ( int ) S1[ 0 ] + ( int ) S2[ 0 ] ) ; for ( int i = 1 ; i <= ( int ) S1[ 0 ] ; i++ ) T[ i] = S1[ i] ; for ( int j = 1 ; j <= ( int ) S2[ 0 ] ; j++ ) T[ j + ( int ) S1[ 0 ] ] = S2[ j] ; return OK; } for ( int i = 1 ; i <= ( int ) S1[ 0 ] ; i++ ) T[ i] = S1[ i] ; for ( int j = 1 ; j <= MAX_SIZE; j++ ) T[ j + ( int ) S1[ 0 ] ] = S2[ j] ; T[ 0 ] = MAX_SIZE; return ERROR;
}
Status SubString ( String Sub, String T, int pos, int len)
{ if ( ( len < 1 ) || ( pos > ( int ) T[ 0 ] ) || ( pos < 1 ) || len > ( int ) T[ 0 ] - pos + 1 ) return ERROR; for ( int i = 1 ; i <= len; i++ ) { Sub[ i] = T[ pos + i - 1 ] ; } Sub[ 0 ] = ( char ) len; return OK;
}
int Index ( String T, String S, int pos)
{ if ( ( S[ 0 ] == 0 ) || ( T[ 0 ] == 0 ) || ( S[ 0 ] > T[ 0 ] ) || ( pos < 0 ) || ( pos > T[ 0 ] ) ) return - 1 ; int i = pos; int j = 1 ; while ( i<= T[ 0 ] && j <= S[ 0 ] ) { if ( T[ i] == S[ j] ) { j++ ; i++ ; } else { i = i - j + 2 ; j = 1 ; } } if ( j > ( int ) S[ 0 ] ) return i - ( int ) S[ 0 ] ; return - 1 ;
}
int Index2 ( String T, String S, int pos)
{ if ( ( S[ 0 ] == 0 ) || ( T[ 0 ] == 0 ) || ( S[ 0 ] > T[ 0 ] ) || ( pos < 0 ) || ( pos > T[ 0 ] ) ) return - 1 ; int n = StrLength ( T) ; int m = StrLength ( S) ; String sub; int i = 1 ; while ( i <= n - m + 1 ) { SubString ( sub, T, i, m) ; if ( StrCompare ( sub, S) == 0 ) return i; i++ ; } return - 1 ;
}
Status StrInsert ( String T, int pos, String S)
{ if ( pos< 1 || pos > S[ 0 ] + 1 ) return ERROR; if ( ( ( int ) S[ 0 ] + ( int ) T[ 0 ] ) > MAX_SIZE) { for ( int i = ( int ) T[ 0 ] ; i >= pos; i-- ) { T[ MAX_SIZE+ i- ( int ) T[ 0 ] ] = T[ i ] ; } for ( int i = 1 ; i <= MAX_SIZE - ( int ) T[ 0 ] ; i++ ) { T[ pos + i - 1 ] = S[ i] ; } T[ 0 ] = MAX_SIZE; return ERROR; } for ( int i = ( int ) T[ 0 ] ; i >= pos; i-- ) { T[ i + ( int ) S[ 0 ] ] = T[ i] ; } for ( int i = 1 ; i <= ( int ) S[ 0 ] ; i++ ) { T[ pos + i - 1 ] = S[ i] ; } T[ 0 ] = ( char ) ( ( int ) T[ 0 ] + ( int ) S[ 0 ] ) ; return OK;
}
Status StrDelete ( String T, int pos, int len)
{ if ( ( pos< 1 ) || ( pos > T[ 0 ] + 1 ) || ( len > T[ 0 ] - pos + 1 ) || ( pos < 0 ) ) return ERROR; for ( int i = pos + len; i <= T[ 0 ] ; i++ ) { T[ i - len] = T[ i] ; } T[ 0 ] = ( char ) ( ( int ) T[ 0 ] - len) ; return OK;
}
Status Replace ( String T, String S, String V)
{ if ( StrEmpty ( S) ) return ERROR; int i = 1 ; while ( i > 0 ) { i = Index ( T, S, i) ; if ( i> 0 ) { StrDelete ( T, i, StrLength ( S) ) ; StrInsert ( T, i, V) ; i += StrLength ( V) ; } } return OK;
}
Status ShowString ( String T)
{ if ( StrLength ( T) == 0 ) return ERROR; for ( int i = 1 ; i <= StrLength ( T) ; i++ ) { printf ( "%c" , T[ i] ) ; } printf ( "\n\n" ) ; return OK;
} int main ( )
{ String str, strcopy, strcat; StrAssign ( str, "abcd" ) ; printf ( "长度:%d, 是否为空:%d(1:是 0:否)\n\n" , StrLength ( str) , StrEmpty ( str) ) ; printf ( "显示str:\n" ) ; ShowString ( str) ; StrCopy ( strcopy, str) ; printf ( "复制str显示strcopy:\n" ) ; ShowString ( strcopy) ; StrAssign ( strcopy, "efghijk" ) ; printf ( "重新显示strcopy:\n" ) ; ShowString ( strcopy) ; printf ( "比较str和strcopy是否相等:%d(0:相等)\n\n" , StrCompare ( str, strcopy) ) ; printf ( "str拼接strcopy:\n" ) ; Concat ( strcat, str, strcopy) ; ShowString ( strcat) ; ClearString ( str) ; printf ( "清空重新显示str:\n" ) ; ShowString ( str) ; printf ( "长度:%d, 是否为空:%d(1:是 0:否)\n\n" , StrLength ( str) , StrEmpty ( str) ) ; SubString ( str, strcat, 2 , 3 ) ; printf ( "截取后重新显示str:\n" ) ; ShowString ( str) ; printf ( "长度:%d, 是否为空:%d(1:是 0:否)\n\n" , StrLength ( str) , StrEmpty ( str) ) ; StrDelete ( str, 1 , 2 ) ; printf ( "删除重新显示str:\n" ) ; ShowString ( str) ; printf ( "长度:%d, 是否为空:%d(1:是 0:否)\n\n" , StrLength ( str) , StrEmpty ( str) ) ; StrInsert ( str, 2 , strcopy) ; printf ( "插入重新显示str:\n" ) ; ShowString ( str) ; printf ( "长度:%d, 是否为空:%d(1:是 0:否)\n\n" , StrLength ( str) , StrEmpty ( str) ) ; int ind = Index ( str, strcopy, 1 ) ; printf ( "查找到了,位置为:%d\n" , ind) ; printf ( "strcat:\n" ) ; ShowString ( strcat) ; printf ( "strcopy:\n" ) ; ShowString ( strcopy) ; printf ( "str:\n" ) ; ShowString ( str) ; Replace ( strcat, str, strcopy) ; printf ( "替换重新显示strcat:\n" ) ; ShowString ( strcat) ; printf ( "长度:%d, 是否为空:%d(1:是 0:否)\n\n" , StrLength ( strcat) , StrEmpty ( strcat) ) ; printf ( "strcat插入一个更长的数:\n" ) ; String s; StrAssign ( s, "123456" ) ; StrInsert ( strcat, 5 , s) ; printf ( "strcat:\n" ) ; ShowString ( strcat) ; printf ( "长度:%d, 是否为空:%d(1:是 0:否)\n\n" , StrLength ( strcat) , StrEmpty ( strcat) ) ; ClearString ( str) ; ClearString ( strcopy) ; ClearString ( strcat) ; printf ( "strcat:\n" ) ; ShowString ( strcat) ; printf ( "strcopy:\n" ) ; ShowString ( strcopy) ; printf ( "str:\n" ) ; ShowString ( str) ; return 0 ;
}