|
是这样的,一个数组,里面存放的东西是不连续的,比如temp[10]={"零","","贰","","","伍","陆","","","玖"}
我想让这个函数能把他连续起来,n是数组的长度。成为{"零","贰","伍","陆","玖","","","","",""}
返回的是现在数组的实际有值的长度,为5
j从2开始是因为我能保证前2个是有值的
int money::pad(int n)
{
int i = 0;
int j = 1;
while(temp[j] != "\0")//j先指向第一个空位置
j++;
while(j < n && i < n)
{
if(temp[i] == "")//让i指向第一个空位置
{
while(temp[j] == "\0")//j再指向空之后的第一个非空位置
j++;
temp[i++] = temp[j];//j填补了i的那个空位置
temp[j++] = '\0';//再将j的位置置空
cout<<i;
}
else
i++;
}
return i;
} |
|