0

質問はタイトルの通りで、この機能を実装するコードを書きました。コードは次のとおりですが、文は次のとおりです。 *(str+length_copy-1+tail_space_num) = *(str+length_copy-1); エラーの原因となります。手を貸していただけますか?どんな種類の答えでも役に立ちます!

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

void replaceSpace(char* str){
if(str == NULL ){
    printf("The parameter is null pointer\n");
}else if(strlen(str) == 0){
    printf("The parameter you parse into is a empty string\n");
}else{
    int length,length_copy,space_num,tail_space_num;
    length=length_copy=strlen(str);
    space_num=tail_space_num =0;
    while(*(str + length -1) == ' '){//' ' is char, but " " is string
        tail_space_num++;
        length--;
    }

    length_copy = length;

    while(length-1>=0){
        if(*(str+length-1) == ' ')
            space_num++;
        length--;
    }
    printf("%d\n",length_copy);
    printf("%d\n",tail_space_num);
    printf("%d\n",space_num);
    if(space_num * 2 != tail_space_num){
        printf("In the tail of the string, there is not enough space!\n");
    }else{
        while((length_copy-1)>=0){
            if(*(str+length_copy-1)!=' '){
                *(str+length_copy-1+tail_space_num) = *(str+length_copy-1);
            }else{
                *(str+length_copy-1+tail_space_num) = '0';
                *(str+length_copy-2+tail_space_num) = '2';
                *(str+length_copy-3+tail_space_num) = '%';
                tail_space_num = tail_space_num -2;
            }
            length_copy --;
        }
    }   
}
} 

main(){
char* str = "Mr John Smith    ";
printf("The original string is: %s\n", str);
printf("the length of string is: %d\n", strlen(str));
replaceSpace(str);
printf("The replaced string is: %s\n", str);
system("pause");    
}
4

1 に答える 1