我需要写一个函数,它接受像"abcdef“这样的字符串,并将其转换为"a-f",或将"589”转换为"5-9",我可以使用stdio.h和string.h中的哪些函数?
发布于 2021-11-12 18:12:41
您需要找到运行的开始和结束位置。如果您认为每个字符都是游程的一部分,那么这会更容易,所以您可能会运行长度为1或更长的游程。
当你找到一次跑步的终点时,你需要根据跑步的长度做不同的事情。
考虑到这一点,我们可以使用以下算法:
循环创建指向string.
1. If the pointed character isn't one more than the saved character,
1. Break.1. If the length of the run is 2+,
1. If the length of the run is 3+,
1. Print a dash. 1. Print the saved character.您应该在纸上运行上面的算法,并使用"4abcz35xy"作为输入。同时,跟踪变量的当前值(指针、游程长度和保存的字符)。
+---+---+---+---+---+---+---+---+---+---+---+
|'4'|'a'|'b'|'c'|'z'|'3'|'4'|'5'|'x'|'y'| 0 |
+---+---+---+---+---+---+---+---+---+---+---+
^
Pointer |
+-------+ |
| -----+
+-------+
Run length
+-------+
| |
+-------+
Saved character
+-------+
| |
+-------+https://stackoverflow.com/questions/69947056
复制相似问题