gpt4 book ai didi

c - 用 '%20' 替换字符串中的所有空格。假设字符串在字符串末尾有足够的空间来容纳额外的字符

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:02:14 26 4
gpt4 key购买 nike

<分区>

问题如题,我已经写了一段代码来实现这个功能。代码如下,但是语句: *(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");
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com