首页 > 代码库 > ctci1.4

ctci1.4

char* newstr(char * str){
    if(str == NULL)
        return NULL;
    int blank = 0;
    int len = strlen(str);
    int i = 0;
    for(i = 0; i < len ; i++)
        if(str[i] ==  )
            blank++;
    int j = 0;
    char * newhead = new char[len + blank*2 +1];
    for(i = 0; i < len; i++){
        if(str[i] !=  )
            newhead[j++] = str[i];
        else{
            newhead[j++] = %;
            newhead[j++] = 2;
            newhead[j++] = 0;
        }
    }
    newhead[j] = \0;
    str = newhead; 
    return str;
}

ctci1.4