[英]string abbreviation characters sequence in C
我需要编写一个函数,它接受像“abcdef”这样的字符串并将其转换为“af”,或将“589”转换为“5-9”,我可以使用 stdio.h 和 string.h 中的哪些函数?
You need to find the start and end of runs.您需要找到运行的开始和结束。 It's easier if you consider every character to be part of a run, so you might runs of length 1 or more.
如果您将每个角色都视为运行的一部分,则更容易,因此您的运行长度可能为 1 或更长。
When you find the end of a run, you need to do different things depending on the length of the run.当你找到跑步的终点时,你需要根据跑步的长度做不同的事情。
With that in mind, we could use the following algorithm:考虑到这一点,我们可以使用以下算法:
You should run through the above algorithm on paper, with "4abcz35xy"
as input.您应该在纸上运行上述算法,输入
"4abcz35xy"
。 While you do, keep track of the current value of the variables (the pointer, the run length and the saved character).执行此操作时,请跟踪变量的当前值(指针、运行长度和保存的字符)。
+---+---+---+---+---+---+---+---+---+---+---+
|'4'|'a'|'b'|'c'|'z'|'3'|'4'|'5'|'x'|'y'| 0 |
+---+---+---+---+---+---+---+---+---+---+---+
^
Pointer |
+-------+ |
| -----+
+-------+
Run length
+-------+
| |
+-------+
Saved character
+-------+
| |
+-------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.