【问题标题】:IndexOf function not working in CIndexOf 函数在 C 中不起作用
【发布时间】:2017-02-02 15:48:07
【问题描述】:

我是 C 指针的新手,我正在尝试编写一个类似于高级编程语言的 String.IndexOf() 函数的程序。

基于String.indexOf function in C,我已经开始工作了:

int main() {
    int index;
    char* source = "test string";
    char* found = strstr( source, "in" );
    if (found != NULL) {
        index = found - source;
    }

    printf("%d\n", index); // prints 8.
    return 0;
}

但是当我尝试将其用作函数时,我总是得到 0。(例如,第一个字符串为“Hello World”,然后“World”将打印“0”而不是预期值“6”)。

基本上,标准输入的第一行是“source”(或“haystack”)字符串,接下来的行将是“needle”。

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

// globals
char master[120];

// returns the index of the substring
int substr(char* needle) {

    int index;
    char* found = strstr( master, needle );
    if (found != NULL) {
        index = found - needle;
    }

    printf("%d\n", index);
    return index;

}

int main() {
    char input[120];
    int timesCalled = 0;

    while(fgets(input, 120, stdin)) {
        timesCalled++;
        if (timesCalled == 1) {
            strcpy(master, input);
        } else {
            substr(input);
        }
    }

    if (timesCalled == 0) {
        fprintf(stderr, "Master String is empty");
        return 1;
    }

    return 0;
}

这里发生了什么? “master”的指针在设置为全局变量时会改变吗? “输入”的指针作为参数传递时会改变吗?为什么它在程序版本中起作用?

感谢任何输入。

编辑!

我已将strcpy(input, master) 行更改为strcpy(master, input),但仍然得到相同的结果!

【问题讨论】:

  • 将代码fgets一行转换成input。然后它strcpy (尚未初始化)master over input。你预计会发生什么?
  • @dxiv 请查看我的编辑 - 我已经交换了 inputmaster 并且仍然得到相同的输出。

标签: c pointers


【解决方案1】:

问题 1

您以错误的顺序将参数传递给strcpy

必须是:

strcpy(master, input);

第一个参数是目标,第二个参数是源。

问题 2

此外,您没有在大海捞针中找到needle,因为fgets() 也会读取换行符。在尝试搜索之前,您需要删除换行符。

问题 3

您使用了错误的指针来计算substr 中的索引。

  index = found - needle;

需要

  index = found - master;

问题 4

您需要将index 初始化为某些东西。否则,当在大海捞针中找不到needle 时,它会返回一个未初始化的值。

int substr(char* needle) {

    int index = -1; // Make the return value negative when needle is not found

    char* found = strstr( master, needle );
    if (found != NULL) {
        index = found - master;
    }

    printf("%d\n", index);
    return index;
}

一个固定的程序

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

// globals
char master[120];

// returns the index of the substring
int substr(char* needle) {

   int index = -1;
   char* found = strstr( master, needle );
   if (found != NULL) {
      index = found - master;
   }

   printf("%d\n", index);
   return index;
}

void removeNewline(char* input)
{
   size_t len = strlen(input);
   if ( input[len-1] == '\n' )
   {
      input[len-1] = '\0';
   }
   else
   {
      printf("No newline found\n");
   }
}

int main() {
   char input[120];
   int timesCalled = 0;

   while(fgets(input, 120, stdin)) {
      removeNewline(input);
      timesCalled++;
      if (timesCalled == 1) {
         strcpy(master, input);
      } else {
         substr(input);
      }
   }

   if (timesCalled == 0) {
      fprintf(stderr, "Master String is empty");
      return 1;
   }

   return 0;
}

输入:

test string
in
es

输出:

8
1

【讨论】:

  • 请查看我的编辑 - 我已经交换了 masterinput 并且得到了相同的输出。什么可能导致这种情况?
  • 感谢您提供解决我所有问题的非常可靠的答案。我注意到当我运行修复了问题 3 的程序时遇到了一些问题,但是 removeNewline() 修复了它!
【解决方案2】:

既然你想要字符串master中的索引,你需要做的不是

if (found != NULL) {
    index = found - needle;
}

是将index = found - needle替换为index = found - master

【讨论】:

  • 你是我的救星!我在我的功能解决方案中错误地转录了链接问题的解决方案。我肯定过头了,仔细检查了一些小东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多