血腥的时代一直坚持这个。似乎没有简单的方法来做到这一点!
希望得到一些帮助,谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char (char *s, const int len) {
{
static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"123456789";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[(sizeof(alphanum) - 6)];
}
s[len] = 0;
memmove(s+pos+1, s+pos, len-pos+1);
s[pos]='-';
puts(s);
}
int main (void)
{
//print the random digits but in the middle need to insert - e.g
//FJ3-FKE
}
最佳答案
有两种简单的方法。
如果您只需要打印那些东西,您可以只在输出中添加破折号,如下所示:
fwrite(s, pos, stdout);
putchar('-');
puts(s+pos);
或者,如果用于 s
的缓冲区足够大以容纳一个 char
,您可以使用 memmove
为破折号,添加破折号,然后打印字符串:
memmove(s+pos+1, s+pos, len-pos+1);
s[pos]='-';
puts(s);
(所有这些都假设 pos
是插入破折号的位置)
关于c - 在字符串中间添加字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17667014/