Implementation of C String

Source: Internet
Author: User

/*
* String. c
*
* Created on: 2012-12-4
* Author: Administrator
*/
# Include <stdio. h>
# Include <stdlib. h>
# Define TRUE 1
# Define FALSE 0
# Define CHECK (t) do {if (! (T) return 0;} while (0)
Typedef char * String;
Typedef int Status;
Static String STRBUFFER = 0;
Int StrLen (String buf ){
String temp = buf;
While (* temp ++ );
Return temp-buf-1;
}
String StrMake (int length ){
Return (String) malloc (length + 1 );
}
String StrChr (String buf, char ch, int length ){
While (length -- & * buf ++! = Ch );
Return buf-1;
}
Void StrCat (String * dest, String src ){
String temp = (String) malloc (StrLen (* dest) + StrLen (src) + 1 );
String head = temp;
String d = * dest;
While (* d ){
* Temp ++ = * d ++;
}
While (* src ){
* Temp ++ = * src ++;
};
* Temp = '\ 0 ';
* Dest = head;
}
Int StrCmp (String buf1, String buf2 ){
Do {
If (* buf1> * buf2) return 1;
If (* buf1 <* buf2) return-1;
If (! * Buf1) break;
Buf1 ++;
Buf2 ++;
} While (1 );
Return 0;
}
Void StrCpy (String * dest, String src ){
* Dest = StrMake (StrLen (src ));
String temp = * dest;
While (* src ){
* Temp ++ = * src ++;
}
* Temp = 0;
}
Void StrFree (String * str ){
Free (* str );
* Str = 0;
}

Status StrPrint (String str ){
CHECK (str );
Puts (str );
Return TRUE;
}
String StrTok (String dest, String src ){
If (dest) StrCpy (& STRBUFFER, dest );
CHECK (src & STRBUFFER & * STRBUFFER );
Int length = StrLen (src );
String ch = StrChr (src, * STRBUFFER, length );
If (* ch = * STRBUFFER ){
Do {
* STRBUFFER ++ = '\ 0 ';
CHECK (* STRBUFFER );
Ch = StrChr (src, * STRBUFFER, length );
} While (* ch = * STRBUFFER );
}
String result = STRBUFFER;
String temp = result;
While (* temp ){
Ch = StrChr (src, * temp, length );
If (* ch = * temp ){
* Temp ++ = '\ 0 ';
CHECK (* temp );
Break;
} Else {
Temp ++;
}
}
STRBUFFER = temp;
Return result & * result? Result: 0;
}
String StrStr (String dest, String src ){
CHECK (dest & src & * dest & * src );
Do {
String temp = src, rt = dest;
While (* temp = * rt ){
Temp ++; rt ++;
}
If (! * Temp) return dest;
CHECK (* rt );
Dest ++;
} While (* dest );
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.