字符串封装

1、main 调用自定义字符串函数,只需要include “MyString.h”即可

#include "myString.h"    
void main(){
   // myString *str; 这个语句错了,我现在是要创建一个myString的结构体变量,而不是结构体指针!!!!
   myString str;
   initString(&str);
   initWithLength(&str,5);
   initWithString(&str,"China is great");
  // addCharAtEnd(&str,'!');
   //addStrAtEnd(&str,"luoxu");
   //deleteFirstChar(&str,'i');
   deleteFirstStr(&str,"is ");
   printString(&str);
   // initWithLength(str,10);
   
}

 2、myString.h //自定义字符串函数声明

//
// Created by luoxu on 8/10/2019.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct CString{
    char *p;   //first address of a string
    int length;  //the length of that string
};
typedef struct CString myString;

//declare self-defined functions about myString
void initString(myString *str );
void initWithLength(myString *str, int len);
void initWithString(myString *str, const char *cpyStr);
void addCharAtEnd(myString *str, char ch);
void deleteFirstChar(myString *str, char ch);
void addStrAtEnd(myString *str, char *addStr);
void deleteFirstStr(myString *str, char *delStr);
void addChar(myString *str, char ch, char pos);  //add ch after pos    
void addStr(myString *str, char *addStr, char pos);  //add string after pos
void printString(myString *str);

 3、realizeMyString.c  自定义函数实现

//
// Created by luoxu on 8/10/2019.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "myString.h"

void initString(myString *str ){
    str->p =NULL;
    str->length =0;
}

void initWithLength(myString *str, int len){
    //str->p = (char *)calloc(len+1, sizeof(char));
    str->p = (char *)calloc(len+1, sizeof(char)); //allocate space with initializing 0
    //str->length = len +1; //do not plus 1 with length, because '\0' is not included,but plus 1 is needed while allocating space like the statement above
    str->length = len;
}

/*void initWithString(myString *str1, char *str2 ){   //str1和str2根本不属于同一个等级
    //用这种方式定义会更好 void initWithString(myString *str, char *cpyStr )
    str1->p = (char *) calloc(strlen(str2)+1, sizeof(char));  // plus 1 is needed while allocating space
    strcpy(str1->p,str2);   //strcpy(str1,str2);这是我犯的错误
    str1->length = strlen(str2);
}*/
void initWithString(myString *str, const  char *cpyStr ){
    str->p = (char *) calloc(strlen(cpyStr)+1, sizeof(char));  // plus 1 is needed while allocating space
    strcpy(str->p,cpyStr);
    str->length = strlen(cpyStr);
}

void addCharAtEnd(myString *str, char ch){
    if(strlen(str->p)+1 == str->length){  //此时就意味着str->p的空间不够了,需要重新分配
        str->p =(char *) realloc(str->p,strlen(str->p)+2);
        *((str->p) + (str->length)) = ch;  //assign last second char
        *((str->p) + (str->length)+1) = '\0';  //assign last char
        str->length++;
    }else{
       //下标法
        str->p[str->length] =ch;
        str->p[str->length+1] ='\0';
    }
}
void deleteFirstChar(myString *str, char ch){
    char * p = str->p;
    while (*(str->p)!= '\0'){
        if(*(str->p) == ch){
            for (char *px = str->p ; px+1 < p+ str->length; ++px) {
                *(px) = *(px+1);
            }
            break ;
        }
        str->p++;
    }
    str->p = p;
    str->length--;
}
void addStrAtEnd(myString *str, char *addStr){
    if(strlen(str->p)+strlen(addStr)+1 > str->length){
        strcat(str->p,addStr);
        str->length += strlen(addStr);
    } else{
        strcat(str->p,addStr);
    }
}

void deleteFirstStr(myString *str, char * delStr){
    char *p = strstr(str->p,delStr);
    char *cpStart =p + strlen(delStr);   //cp every char from here
    if(*cpStart == '\0'){  //if cpStart is already at End, then end the string
        *p = '\0';   //end    the string
    } else{
        while (*cpStart != '\0'){
            *p = *cpStart;
            p++;
            cpStart++;
        }
        *p = '\0';  //if(*cpStart == '\0') them end the string

    }

}
void printString(myString *str){
    printf("print string:%s\n",str->p);
    printf("print len:%d\n",str->length);
}

 

上一篇:C++右值引用


下一篇:造*:新建一个属于自己的String类