user-0 提问: 关于字符串的程序，求教高手

回复:
user: user-0
content: 引用 1楼赵4老师 的回复://有一塊內存區域中連續存放着若干個以NULL結尾的字符串，這塊區域以兩個NULL字節結束(包括最後一個字符串的結尾NULL字節)，完成以下要求： //1)  寫一個函數名為SplitString，用來得到每個字符串的地址和字符串大小以及字符串個數（50分） #include <stdio.h> #include <string.h> typedef struct {     unsigned int offset;         //字符串在內存區域中的偏移量     unsigned int length;        //字符串長度(不包括NULL) } STR_LIST;  STR_LIST  *SplitString(char *strs, unsigned int *strCount) {     char *p;     int n,L;     STR_LIST *sl;      p=strs;     n=0;     while (1) {         if (!*p && !*(p+1)) {n++;break;}         if (!*p) n++;         p++;     }     *strCount=n;      sl=new STR_LIST[n];      p=strs;     n=0;     L=0;     sl[n].offset=0;     while (1) {         if (!*p && !*(p+1)) {             sl[n].length=L;             break;         }         if (!*p) {             sl[n].length=L;             L=-1;             sl[n+1].offset=p-strs+1;             n++;         }         p++;         L++;     }     return sl; } //2)  寫一個函數名為ReplaceString，用來將其中的某個字符串(從1開始數)替換成指定的字符串，失敗返回0，成功返回1 （50分） unsigned char ReplaceString(char *strs, unsigned int strNum, char *strReplace) {     STR_LIST *StrList = NULL;     int L1,L2,i,MoveLen;     unsigned int strCount = 0;      StrList=SplitString(strs, &strCount);     if (0<strNum && strNum<=strCount) {         L1=strlen(strReplace);         L2=StrList[strNum-1].length;         if (L1!=L2) {             MoveLen=2;             for (i=strNum;i<(int)strCount;i++) MoveLen+=StrList[i].length+1;             if (strNum<strCount) {                 memmove(strs+StrList[strNum].offset-1+L1-L2,strs+StrList[strNum].offset-1,MoveLen);             } else {                 strs[StrList[strNum-1].offset+L1]=0;                 strs[StrList[strNum-1].offset+L1+1]=0;             }         }         for (i=0;i<L1;i++) strs[StrList[strNum-1].offset+i]=strReplace[i];         delete[] StrList;         return 1;     } else {         delete[] StrList;         return 0;     } } void DisplayString(char *strs) {     char *p;      p=strs;     while (1) {         printf("%s\\0",p);         p=p+strlen(p);         if (!*p && !*(p+1)) break;         p++;     }     printf("\n"); } int main() {     unsigned int i;     unsigned int strCount = 0;     STR_LIST *StrList = NULL;     char strs[] =  "abc\0def\0gh\0ijklmnopq\0rstu\0vwxyz\0" ;     StrList = SplitString(strs, &strCount);     for (i = 0; i < strCount; i++) {         printf( "str #%d length = %d, value = %s\n" , i, StrList[i].length, strs + StrList[i].offset);     }     delete[] StrList;     unsigned char r;     r=ReplaceString(strs,10,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     r=ReplaceString(strs,1,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     r=ReplaceString(strs,3,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     r=ReplaceString(strs,4,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     r=ReplaceString(strs,6,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     r=ReplaceString(strs,7,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     return 0; } //str #0 length = 3, value = abc //str #1 length = 3, value = def //str #2 length = 2, value = gh //str #3 length = 9, value = ijklmnopq //str #4 length = 4, value = rstu //str #5 length = 5, value = vwxyz //r==0 //abc\0def\0gh\0ijklmnopq\0rstu\0vwxyz\0 //r==1 //XYZ\0def\0gh\0ijklmnopq\0rstu\0vwxyz\0 //r==1 //XYZ\0def\0XYZ\0ijklmnopq\0rstu\0vwxyz\0 //r==1 //XYZ\0def\0XYZ\0XYZ\0rstu\0vwxyz\0 //r==1 //XYZ\0def\0XYZ\0XYZ\0rstu\0XYZ\0 //r==0 //XYZ\0def\0XYZ\0XYZ\0rstu\0XYZ\0 // 老师，可以讲解一下这题的思路么？谢谢！

user: user-1
content: //有一塊內存區域中連續存放着若干個以NULL結尾的字符串，這塊區域以兩個NULL字節結束(包括最後一個字符串的結尾NULL字節)，完成以下要求： //1)  寫一個函數名為SplitString，用來得到每個字符串的地址和字符串大小以及字符串個數（50分） #include <stdio.h> #include <string.h> typedef struct {     unsigned int offset;         //字符串在內存區域中的偏移量     unsigned int length;        //字符串長度(不包括NULL) } STR_LIST;  STR_LIST  *SplitString(char *strs, unsigned int *strCount) {     char *p;     int n,L;     STR_LIST *sl;      p=strs;     n=0;     while (1) {         if (!*p && !*(p+1)) {n++;break;}         if (!*p) n++;         p++;     }     *strCount=n;      sl=new STR_LIST[n];      p=strs;     n=0;     L=0;     sl[n].offset=0;     while (1) {         if (!*p && !*(p+1)) {             sl[n].length=L;             break;         }         if (!*p) {             sl[n].length=L;             L=-1;             sl[n+1].offset=p-strs+1;             n++;         }         p++;         L++;     }     return sl; } //2)  寫一個函數名為ReplaceString，用來將其中的某個字符串(從1開始數)替換成指定的字符串，失敗返回0，成功返回1 （50分） unsigned char ReplaceString(char *strs, unsigned int strNum, char *strReplace) {     STR_LIST *StrList = NULL;     int L1,L2,i,MoveLen;     unsigned int strCount = 0;      StrList=SplitString(strs, &strCount);     if (0<strNum && strNum<=strCount) {         L1=strlen(strReplace);         L2=StrList[strNum-1].length;         if (L1!=L2) {             MoveLen=2;             for (i=strNum;i<(int)strCount;i++) MoveLen+=StrList[i].length+1;             if (strNum<strCount) {                 memmove(strs+StrList[strNum].offset-1+L1-L2,strs+StrList[strNum].offset-1,MoveLen);             } else {                 strs[StrList[strNum-1].offset+L1]=0;                 strs[StrList[strNum-1].offset+L1+1]=0;             }         }         for (i=0;i<L1;i++) strs[StrList[strNum-1].offset+i]=strReplace[i];         delete[] StrList;         return 1;     } else {         delete[] StrList;         return 0;     } } void DisplayString(char *strs) {     char *p;      p=strs;     while (1) {         printf("%s\\0",p);         p=p+strlen(p);         if (!*p && !*(p+1)) break;         p++;     }     printf("\n"); } int main() {     unsigned int i;     unsigned int strCount = 0;     STR_LIST *StrList = NULL;     char strs[] =  "abc\0def\0gh\0ijklmnopq\0rstu\0vwxyz\0" ;     StrList = SplitString(strs, &strCount);     for (i = 0; i < strCount; i++) {         printf( "str #%d length = %d, value = %s\n" , i, StrList[i].length, strs + StrList[i].offset);     }     delete[] StrList;     unsigned char r;     r=ReplaceString(strs,10,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     r=ReplaceString(strs,1,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     r=ReplaceString(strs,3,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     r=ReplaceString(strs,4,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     r=ReplaceString(strs,6,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     r=ReplaceString(strs,7,"XYZ");     printf("r==%d\n",r);DisplayString(strs);     return 0; } //str #0 length = 3, value = abc //str #1 length = 3, value = def //str #2 length = 2, value = gh //str #3 length = 9, value = ijklmnopq //str #4 length = 4, value = rstu //str #5 length = 5, value = vwxyz //r==0 //abc\0def\0gh\0ijklmnopq\0rstu\0vwxyz\0 //r==1 //XYZ\0def\0gh\0ijklmnopq\0rstu\0vwxyz\0 //r==1 //XYZ\0def\0XYZ\0ijklmnopq\0rstu\0vwxyz\0 //r==1 //XYZ\0def\0XYZ\0XYZ\0rstu\0vwxyz\0 //r==1 //XYZ\0def\0XYZ\0XYZ\0rstu\0XYZ\0 //r==0 //XYZ\0def\0XYZ\0XYZ\0rstu\0XYZ\0 //