gpt4 book ai didi

c - 交换 "C"中句子中的单词

转载 作者:太空宇宙 更新时间:2023-11-04 04:12:41 25 4
gpt4 key购买 nike

我有一个学校的作业,我必须做一个项目,它从 CMD 获取参数,每个参数都有两个单词,用 / 分隔,然后我们插入句子和程序应该找到所有出现的第一个单词并将其替换为第二个单词,我只是试图使用 strtok_r 使其工作。

这是我的代码:

#include <stdio.h>
#include <string.h>
#include <conio.h>

int main(int args, char *argv[]) {
char *token;
char *token2;
char veta[1000];
int i, j, k;
int err;
char *saveptr1, *saveptr2;

if (fgets(veta, 1000, stdin)) {
token = strtok_r(veta, " \n", &saveptr1);
k = 1;
while (token != NULL) {
printf("Cycle %d: \n", k++);
printf("%s\n", token);
for (i = 1; i < args; i++) {
token2 = strtok_r(argv[i], "/", &saveptr2);
printf("%s ", token2);
token2 = strtok_r(NULL, "/", &saveptr2);
printf("%s \n", token2);
}
token = strtok_r(NULL, " \n", &saveptr1);
}
}
return(0);
}

当我在 CMD 中输入参数并插入带有例如四个单词的句子时,执行了 4 个循环但输出不是我想要的......例如当我给出这些参数时:hi/hello how/good no/yet 并插入句子输出为:

Cycle1: 
(first word of sentence)
hi hello
how good
no yet
Cycle4:
(second word of sentence)
hi (null)
how (null)
no (null)
Cycle4:
(third word of sentence)
hi (null)
how (null)
no (null)
Cycle4:
(fourth word of sentence)
hi (null)
how (null)
no (null)

应该是这样的:

Cycle1: 
(first word of sentence)
hi hello
how good
no yet
Cycle4:
(second word of sentence)
hi hello
how good
no yet
Cycle4:
(third word of sentence)
hi hello
how good
no yet
Cycle4:
(fourth word of sentence)
hi hello
how good
no yet

我可能无法修复它,你能帮我吗?

提前致谢。

最佳答案

如我所说strtok_r修改它的第一个参数,需要先保存argv

中的单词

我也鼓励您不要修改argv,并检查参数是否正确检查strtok 的结果。或 strtok_r


例如你可以这样做:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
char ** first = malloc((argc - 1) * sizeof(char *));
char ** second = malloc((argc - 1) * sizeof(char *));
int i;

for (i = 1; i != argc; ++i) {
char * s = strdup(argv[i]);

if (((first[i - 1] = strtok(s, "/")) == NULL) ||
((second[i - 1] = strtok(NULL, "/")) == NULL)) {
fprintf(stderr, "invalid argument '%s'\n", argv[i]);
return -1;
}
/* debug */
else
printf("first[%d]='%s' second[%d]='%s'\n", i-1, first[i-1], i-1, second[i-1]);

}

/*
here you read sentences and do the substitutions
*/

/* free resources */
for (i = 0; i != argc - 1; ++i) {
free(first[i]);
}
free(first);
free(second);

return(0);
}

编译和执行:

/tmp % gcc -g -pedantic -Wall -Wextra c.c
/tmp % ./a.out hi/hello how/good no/yet
first[0]='hi' second[0]='hello'
first[1]='how' second[1]='good'
first[2]='no' second[2]='yet'

valgrind 下执行:

/tmp % valgrind ./a.out hi/hello how/good no/yet
==29457== Memcheck, a memory error detector
==29457== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==29457== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==29457== Command: ./a.out hi/hello how/good no/yet
==29457==
first[0]='hi' second[0]='hello'
first[1]='how' second[1]='good'
first[2]='no' second[2]='yet'
==29457==
==29457== HEAP SUMMARY:
==29457== in use at exit: 0 bytes in 0 blocks
==29457== total heap usage: 5 allocs, 5 frees, 73 bytes allocated
==29457==
==29457== All heap blocks were freed -- no leaks are possible
==29457==
==29457== For counts of detected and suppressed errors, rerun with: -v
==29457== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

注意这是可能的

  char * first[argc - 1];
char * second[argc - 1];

而是在堆中分配 firstsecond,但是编译时堆栈中的数组大小未知,我建议您不要使用这种方式

关于c - 交换 "C"中句子中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55489646/

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