struct number {char digits[11];};
Run Code Online (Sandbox Code Playgroud)
以下方法从(*a).digits中删除前导零
void remove_zero(struct number *a);
Run Code Online (Sandbox Code Playgroud)
示例:(*a).digits 000013204 ---> 13204
我的方法是定义一个变量b等于(*a).digits,开始搜索b中的第一个非零数字,然后将(*a).digits替换为b的其余部分.但是,我在实现代码时遇到了麻烦
void remove_zero(struct number *a) {
char b = (*a).digits;
while (b){ // <--- (b) indicates that it hasnt reached the terminator,right?
if (b != '0')
{ //<-- here is where to replace (*a).digits with the rest of b, but how to?
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以你有一个包含例如的数组
+---+---+---+---+---+---+---+---+---+---+---+ | 0 | 0 | 0 | 0 | 1 | 3 | 2 | 0 | 4 | \0| | +---+---+---+---+---+---+---+---+---+---+---+
而你希望它包含
+---+---+---+---+---+---+---+---+---+---+---+ | 1 | 3 | 2 | 0 | 4 | \0| | | | | | +---+---+---+---+---+---+---+---+---+---+---+
从上面的"图像"中可以清楚地看出,这可以通过简单的数据移动来完成.
因此,一种解决方案是找到第一个非零字符,然后从该字符移动到数组的开头.
| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |