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;
}
posted @ 2014-10-09 13:58  lycan785  阅读(171)  评论(0编辑  收藏  举报