字符串中空格替换(C语⾔实现)
题⽬:请实现⼀个函数,把字符串中的每个空格替换成 "%20"。例如,输⼊ "We are happy.", 则输出 "We%20are%20happy." 。char formatBlank(char arr[])
{
if(arr == NULL)
{
return 0;
}
int originLength = 0;
int blankLength = 0;
int newLength = 0;
int i = 0;
while(arr[i] != '\0')
{
originLength ++;
if(arr[i] == ' ')
{
blankLength ++;
}
i ++;
}
字符串转数组去除空格newLength = originLength + blankLength * 2;
for(int j = originLength; j > 0 ; j--)
{
if(arr[j] != ' ')
{
arr[newLength--] = arr[j];
}else {
arr[newLength--] = '0';
arr[newLength--] = '2';
arr[newLength--] = '%';
}
}
return 0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。