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

标签 c pointers strtok

我有一个学校的作业,我必须做一个项目,它从 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/

相关文章:

c - 信号量互斥锁防止死锁 - C 程序

c++ - 在 C++ 中递增 char 指针

c - 如何使用模式匹配分割字符串?

c++ - Sublime Text 启动单独的命令窗口(C/C++)

c - 保留执行管道

c - select() 在非阻塞 AF_PACKET 套接字上立即返回 0,忽略 timeval

c++ - 矩阵指针语法

c - 在函数指针数组中设置值时函数参数太少

c - C 中的 strtok 函数

c - 当一行中有 2 个分隔符时,将字符串拆分为 C 中的标记