gpt4 book ai didi

c - 字符串中的单词排序

转载 作者:行者123 更新时间:2023-11-30 20:27:21 25 4
gpt4 key购买 nike

我正在尝试创建一个函数,从用户那里获取一个字符串和一个数字,然后使每个单词的长度与我从用户那里获得的数字相同并打印新字符串。
例如:

abcd__efgh_i
number = 3
and i should get
abc_def_ghi

#include < stdio.h >
void f(char * p, int n) {
int i = 0, br = 0, d, m = 0, br1 = 0, g;
while (p[i] != '\0') {
if (p[i] != '\0') {
br++;
}
i++;
}
if (br % n == 0) {
d = (br / n) - 1;
} else {
d = (br / n);
}
g = br + d;
char b[g];
i = 0;
while (p[i] == '\0') {
if (p[i] == '\0') {
while (p[i] == '\0') {
i++;
}
} else {
b[m] = '\0';
m++;
br1 = 0;
}
b[m] = p[i];
m++;
i++;
br1++;
if (m == g) {
b[m] = '\0';
}
}

printf("%s", b);
}

最佳答案

#include <stdio.h>

void
shuffle (const char *str, unsigned int n) {
unsigned int i = 0;

// loop until the end of the string is found
while (*str) {

// Examine the current character. If it is not a space, output it
// and increment the number of characters output so far in the current word
if (*str != ' ') {
putchar (*str);
++i;
}

// Move to the next character
++str;

// Check if we have output "n" characters so far. If so, output a space to
// separate words and reset the character count
if (i == n) {
putchar (' ');
i = 0;
}
}
// Put a single new line at the end
putchar ('\n');
}

int
main () {
shuffle ("abcd efgh i jk", 3);
}

关于c - 字符串中的单词排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20595319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com