交换句子中“ C”中的单词

时间:2019-04-03 07:42:47

标签: 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

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

谢谢。

1 个答案:

答案 0 :(得分:0)

正如我在评论strtok_r中说的那样,修改其第一个参数,您需要首先保存 argv

中的单词

我也建议您不要修改 argv ,并检查参数是否正确,检查strtokstrtok_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];

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