// string functions implemented independent #include <stdio.h> #include <stdlib.h> #include <string.h> ////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// //////////////// void myStrrev (char * P) {// string reversal int length = strlen (P); int tmp = 0; for (int I = 0; I <length / 2; I ++) { tmp = P [I]; P [I] = P [I-length-. 1]; P [-length-I. 1] = tmp; } } void main11 () {/ / string reversal char STR [18 is] = "China Great IS"; // strrev (STR); myStrrev (STR); the printf ( "% S \ n-", STR); // Si anihC taerg } ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////// // string case conversion void myStrupr (char * P) { ! the while (* P = '\ 0') { IF (* P> = 'A' && * P <= 'Z') { * P = * P - 32; } P ++; } } void myStrlwr ( char * P) { ! the while (* P = '\ 0') { IF (* P> = 'A' && * P <= 'the Z') { * P = * P + 32; } P ++; } } void main12 () {// string case conversion char STR [18 is] = "China Great IS"; // strupr (STR); myStrupr (str); the printf ( "% S \ n-", STR); the IS GREAT CHINA // // strlwr (STR); myStrlwr (STR); the printf ( "% S \ n-", STR); // China Great IS } /// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////////////////////// int myStrlen (char * P) {// find the string length int I = 0; the while (! * P = '\ 0') { P ++; I ++; } return I; } void main13 () {// find the string length char STR [18 is] = "China IS Great"; the printf ( "% D \ n-", strlen (STR)); // 14 the printf ( "% D \ n-", myStrlen (STR)); // 14 } ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////// void myStrcpy (dest char *, char * Source) { // string copy request IF (dest == NULL || Source == NULL) { return; } {the else the while (Source = NULL!) { * Source * = dest; dest ++; Source ++; } * dest = '\ 0 '; dest // [0] =' \ 0 '; } } void main14 () {// find string copy char str1 [18 is] = "China Great IS"; char str2 [20 is]; // strcpy (str2 , str1); myStrcpy (str2, str1); the printf ( "% S \ n-", str2);//China is great } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void myStrcat(char *dest, char*source){ //求字符串连接 if(dest == NULL || source == NULL){ return; } else{ //char *p =(char *) realloc(dest,strlen(dest)+strlen(source)+1); int destLen = strlen(dest); dest = dest +destLen; while (*source != '\0'){ *dest =*source; dest++; source++; } *dest = '\0'; //dest[0] = '\0'; } } void main15(){ //求字符串连接 char str1[18] ="China is"; str2 char [20 is] = "Great"; // strcat (str1, str2); myStrcat (str1, str2); the printf ( "% S \ n-", str1); // China Great IS } ////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////// char * myStrchr (char * str, char CH) {// Find character while (* str ! = '\ 0') { IF (STR * == CH) { return STR; } STR ++; } return NULL; } void main16 () {// find the character char STR [18 is] = "China Great IS"; char = CH 'A'; char * P = the strchr (STR, CH); // find its return position, Van not found return NULL the printf ( "% S \ n-", P);//a is great myStrchr = P (STR, CH); the printf ( "% S \ n-", P);//a is great } //////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////// int myStrcmp (char * str1, char * str2) { // character compare the while (* * str1 == str2) { IF (* str1 == '\ 0' && * str2 == '\ 0') { return 0; } str1 ++; str2 ++ ; } return -1; } void main17 () {// character comparison char * str1 = "China Great IS"; char * str2 = "China Great IS"; / * IF (str1 == str2) {// C language such string can not compare, str1 str2 and address are the printf ( "equality"); } * / IF (strcmp (str1,str2) == 0) { the printf ( "equality"); } IF (myStrcmp (str1, str2) == 0) { the printf ( "equality"); } } ///////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////// char * myStrstr (str1 char *, char * str2) {// find the string may be represented by the following table method, an array manner char * p = str1 ; // initialize pointer can void * p; void pointer type can be converted to any type // char * P = (char *) P; char * str2 = str2Begin; the while (* str1 = '\ 0'!) { p = str1; // to preserve the position of the first character in str1 str2 appearing in later return str2 = str2Begin; // the while (* str2 str1 == * || * str2 == '\ 0') {// * str2 == '\ 0 ' to the last bit of the data comparison, can enter if Analyzing returns p, into an infinite loop to prevent if (* str2 == '\ 0 ') { P return; } str2 ++; // str1 str2 and the first character is equal to the backward movement continues str1 ++; } str1 = P; // Do not miss this step, in the process of the sub-string comparison, has str1 moving back a big str1 ++; // not found on the main stream continues to move back } } void main () {// find the string char * str1 = "Great Chiniiss iS"; char * str2 = "iS" ; char * P = Strstr (str1, str2); the printf ( "% S \ n-", P); char * = myStrstr the myP (str1, str2); the printf ( "% S \ n-", the myP); }