#include <stdio.h>
#include <stdlib.h> #define MAXLEN 255
typedef struct
{ char * ch; int length;
} SString;
bool StrAssign ( SString& T, char * chars)
{ int i = 0 , len; T. ch = ( char * ) malloc ( MAXLEN) ; char * p; for ( len = 0 , p = chars; * p != '\0' ; p++ , len++ ) ; if ( len == 0 ) { T. length = 0 ; return false ; } else { for ( i = 0 ; i < len; i++ ) { T. ch[ i + 1 ] = * ( chars + i) ; } T. length = len; } return true ; }
bool StrCopy ( SString& T, char * s)
{ int i = 0 , len; char * p; for ( len = 0 , p = s; * p != '\0' ; p++ , len++ ) ; if ( len == 0 ) { T. length = 0 ; } else { for ( i = 0 ; i < len; i++ ) { T. ch[ i + 1 ] = * ( s + i) ; } T. length = len; } return true ; }
bool StrEmpty ( SString& T)
{ if ( T. length == 0 ) return true ; return false ;
}
int StrCompare ( SString S, SString T)
{ int i; for ( i = 1 ; i <= S. length && i <= T. length; i++ ) { if ( S. ch[ i] != T. ch[ i] ) { return S. ch[ i] - T. ch[ i] ; } } return S. length - T. length; }
int Strlength ( SString S)
{ return S. length; } bool Concat ( SString& T, SString S, SString S1)
{ int i, j; if ( S. length + S1. length > MAXLEN) { return false ; } else { for ( i = 0 ; i < S. length; i++ ) { T. ch[ i + 1 ] = S. ch[ i + 1 ] ; } for ( j = 0 ; j < S1. length; j++ ) { T. ch[ i + j + 1 ] = S1. ch[ j + 1 ] ; } } T. length = S. length + S1. length; return true ; } bool SubString ( SString& Sub, SString& S, int pos, int len)
{ if ( pos + len - 1 > S. length) { return false ; } for ( int i = pos; i < pos + len; i++ ) { Sub. ch[ i - pos + 1 ] = S. ch[ i] ; } Sub. length = len; return true ; } bool ClearString ( SString& S)
{ S. length = 0 ; return true ;
} int Index ( SString T, SString S)
{ int m = Strlength ( T) ; int n = Strlength ( S) ; SString q; q. ch= ( char * ) malloc ( MAXLEN) ; int i = 1 ; while ( i <= m - n + 1 ) { SubString ( q, T, i, n) ; if ( StrCompare ( q, S) == 0 ) { return i; } i++ ; } return - 1 ; } int main ( )
{ SString T; SString S; SString S1; char s[ 20 ] = "chen" ; char s1[ 20 ] = "en" ; char s2[ 20 ] = "hen" ; StrAssign ( T, s) ; StrAssign ( S, s2) ; StrAssign ( S1, s1) ; int ret= StrCompare ( S, T) ; if ( ret > 0 ) { printf ( "s big\n" ) ; } else if ( ret == 0 ) { printf ( "s与t相等\n" ) ; } else { printf ( "t大\n" ) ; } for ( int i = 0 ; i < T. length; i++ ) { printf ( "%c" , T. ch[ i + 1 ] ) ; } return 0 ;
}