IndexOf函数在C中不起作用

时间:2016-09-25 04:58:13

标签: c pointers

我是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"将打印&#34 ; 0"而不是期望值" 6")。

基本上,stdin的第一行是" 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;
}

这里发生了什么?指针是&#34; master&#34;当它被设置为全局变量时会改变吗?指针是&#34;输入&#34;当它作为参数传递时改变?为什么它在程序版本中有效?

任何输入都表示赞赏。

修改<!/强>

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

由于您需要字符串master中的索引,因此您需要执行的操作而不是

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

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