C 字符串中的单词排序
我正在尝试创建一个函数,该函数从用户处获取字符串和数字,然后使每个单词的长度与我从用户处获取的数字相同,并打印新字符串。C 字符串中的单词排序,c,string,C,String,我正在尝试创建一个函数,该函数从用户处获取字符串和数字,然后使每个单词的长度与我从用户处获取的数字相同,并打印新字符串。 例如: 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') {
例如:
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);
}
abcd\u\u efgh\u i
数字=3
我应该
abc_def_ghi
#包括
空f(字符*p,整数n){
int i=0,br=0,d,m=0,br1=0,g;
而(p[i]!='\0'){
如果(p[i]!='\0'){
br++;
}
i++;
}
如果(br%n==0){
d=(br/n)-1;
}否则{
d=(br/n);
}
g=br+d;
字符b[g];
i=0;
而(p[i]='\0'){
如果(p[i]='\0'){
而(p[i]='\0'){
i++;
}
}否则{
b[m]='\0';
m++;
br1=0;
}
b[m]=p[i];
m++;
i++;
br1++;
如果(m==g){
b[m]='\0';
}
}
printf(“%s”,b);
}
#包括
无效的
洗牌(常量字符*str,无符号整数n){
无符号整数i=0;
//循环,直到找到字符串的结尾
while(*str){
//检查当前字符。如果它不是空格,则输出它
//并增加当前单词中迄今为止输出的字符数
如果(*str!=''){
putchar(*str);
++一,;
}
//移动到下一个角色
++str;
//检查到目前为止是否已输出“n”个字符。如果是,请向输出一个空格
//分离单词并重置字符计数
如果(i==n){
putchar(“”);
i=0;
}
}
//在末尾放一行新词
putchar('\n');
}
int
主要(){
洗牌(“abcd efgh i jk”,3);
}
请编辑您的问题以修正代码缩进,使其可读。谢谢所以问题是什么?它没有正常工作。我在问如何正确操作。这是我第一次在这里寻求帮助,所以请小心任何错误。第一个计时器?欢迎来到SO!您的状态表示您尚未阅读该页。请尽快这样做。有关如何提出适当问题的提示,请查看。没有解释吗?复制+粘贴=理解?我认为这些评论很好地解释了正在发生的事情。
void f(const char * p, int n) {
char b[n+1];
int i=0;
while(*p){
if(*p == ' '){
++p;
continue;//skip
}
b[i++] = *p++;
if(i % n == 0){
b[i] = '\0';//move out from loop
printf("%s ", b);
i = 0;
}
}
printf("\n");
}
#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);
}