/************************************
名 称:CString
作 者:freewind
版 本:v1.0
时 间:2006-08
Email:freewind22@163.com
*************************************/
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
/************************************ StrLength **********************************/
int StrLength( const char *str )
{
int len;
for( len=0; *str++; len++ );
return len;
}
/************************************ StrAssgin **********************************/
char *StrAssign( char *dest, const char *sour )
{
int len;
char *ret;
len=StrLength( sour );
ret=dest=(char *)malloc(len+1);
if( !dest )
printf("内存分配失败!/n");
else
while( *dest++ = *sour++ );
return ret;
}
/************************************ StrCopy **********************************/
char *StrCopy( char *dest, const char *sour )
{
char *ret=dest;
if( !dest || !sour )
printf("源串或目标串为空!/n");
else
while( *dest++ = *sour++ );
return ret;
}
/************************************ StrEmpty **********************************/
int StrEmpty( const char *str )
{
if( str )
return *str?FALSE:TRUE;
else
return TRUE;
}
/************************************ StrCompare *******************************/
int StrCompare( const char *str1, const char *str2 )
{
int result=0;
if( str1!=str2 )
while( (*str1 || *str2) && !(result=*str1++ - *str2++) );
return result;
}
/************************************ StrConcat ********************************/
char *StrConcat( char *dest, const char *sour )
{
char *ret=dest;
while( *dest ) dest++;
while( *dest++ = *sour++ );
return ret;
}
/************************************ SubString *********************************/
char *SubString( char *dest, const char *sour, int start, int len )
{
char *ret=dest;
int i;
for(i=0; i<start && *sour++; i++);
for(i=0; i<len && *sour; i++)
*dest++ = *sour++;
*dest=0;
return ret;
}
/************************************ StrIndex *********************************/
int StrIndex( const char *str, const char *substr, int start )
{
int i, j, result=-1;
for( i=start,j=0; str[i] && substr[j]; i++,j++ )
{
if( str[i] !=substr[j] )
{
start++;
i=start;
j=0;
}
}
if( !substr[j] )
result=start;
return result;
}
/************************************ StrReplace **********************************/
int StrReplace( char *str, const char *rep, const char *des )
{
int pos=0, len1, len2;
int i, c, count=0;
char *p,*q;
pos=StrIndex( str, rep, pos );
len1=StrLength( rep );
len2=StrLength( des );
c=len1-len2;
while( pos>=0 )
{
p=&str[pos+len2];
if( len1 > len2 ) /* <-- */
{
while( *p=*(p+c) )
{
p++;
}
}
else if( len1 < len2 ) /* --> */
{
c=-c;
q=&str[StrLength( str )+c];
while( q>=p )
{
*q=*(q-c);
q--;
}
}
/*replace */
count++;
p=&str[pos];
/*printf("%s/n/n", p );*/
for(i=0;i<len2;i++)
*p++=des[i];
pos+=len2+1;
pos=StrIndex( str, rep, pos );
}
return count;
}
/************************************ StrInsert **********************************/
void StrInsert( char *str, int pos, const char *instr )
{
int len;
char *p,*q;
len=StrLength( instr );
p=&str[pos];
q=&str[StrLength( str ) + len];
while( q>=p )
{
*q=*(q-len);
q--;
}
while( *instr )
*p++=*instr++;
}
/************************************ StrDelete **********************************/
void StrDelete( char *str, int pos, int len )
{
char *p;
p=&str[pos];
while( *p=*(p+len) )
p++;
}
/************************************** main ***********************************/
void main()
{
char str[50]="I am a student";
char s1[20]="abd",s2[20]="abcd",s3[]="abcde",s4[]="abcd";
char str2[20];
char *s=0;
const char *ss;
int index;
printf("-----------------StrCopy StrEmpty StrLength ---------------/n");
StrCopy( str2, str );
printf("%s /n%s /n%d %d/n",str,str2,StrLength(str), StrLength(str2));
printf("%d %d /n",StrEmpty(s),StrEmpty(str) );
printf("--------------------StrCompare StrAssign ------------------/n");
printf("%s %s %s %s/n",s1,s2,s3,s4);
printf("%d %d %d %d/n",StrCompare(s1,s2),StrCompare(s2,s3),StrCompare(s1,s3),StrCompare(s2,s4));
s=StrAssign( s,str+2 );
printf("%s /n%d /n",s,StrLength(s) );
printf("--------------------StrConcat SubString -------------------/n");
StrConcat( s1, s2 );
printf("s1+s2=%s/n",s1 );
SubString(s1,str,5,10);
ss=SubString(s2,str,0,20);
printf("%s %s/n",s1,s2);
printf("----------------------StrIndex StrReplace -------------------/n");
index=StrIndex( str, "am", 0 );
printf("%d %d %d/n", index, StrIndex( str, "Kk", 0 ), StrIndex( "ababab","ab",1 ) );
printf("%s/n", str );
index=StrReplace( str, "am", "are" );
printf("%d %s ",index, str );
index=StrReplace( str, "are", "am" );
printf("%d %s ",index, str );
index=StrReplace( str, "am", "AM" );
printf("%d %s /n",index, str );
printf("----------------------StrInsert StrDelete --------------------/n");
StrInsert( str, 4, " not" );
printf("%s ", str );
StrDelete( str, 4, 4 );
printf("%s /n", str );
free(s);
}