Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中查找数组的索引_C_Arrays_Recursion_Indexing - Fatal编程技术网

在C中查找数组的索引

在C中查找数组的索引,c,arrays,recursion,indexing,C,Arrays,Recursion,Indexing,我想写一个代码,可以让我找到一个字母的首字母出现的位置,这就是我到目前为止所想到的。如您所见,函数返回的实际上是一个值,而不是索引。有没有一种方法可以找到它,而不必像代码2那样简单地给出索引的初始值 char *recFirstPosition(char * source, int letter) { if(*source == letter) return *source; if (*source =='\0') return 0;

我想写一个代码,可以让我找到一个字母的首字母出现的位置,这就是我到目前为止所想到的。如您所见,函数返回的实际上是一个值,而不是索引。有没有一种方法可以找到它,而不必像代码2那样简单地给出索引的初始值

char *recFirstPosition(char * source, int letter)
{

     if(*source == letter)
        return *source;

     if (*source =='\0')
      return 0;

     recFirstPosition(++source, letter);
}


char *recFirstPosition(char * source, int letter, int index)
{

     if(*(source+index) == letter)
        return index;

     if (*(source+index) =='\0')
      return 0;

     recFirstPosition(source, letter, ++index);
}

只需从第一个返回中分离*并为第一个版本的递归调用添加返回

char *recFirstPosition(char * source, int letter)
{

     if(*source == letter)
        return source;

     if (*source =='\0')
      return 0;

     return recFirstPosition(++source, letter);
}
它将使代码正常工作。您的第一个版本会导致类型错误

以下版本比上述版本更具可读性:

char *recFirstPosition(char *source, char letter)
{
    if (*source == '\0')
        return NULL;
    else if (*source == letter)
        return source;
    else
        return recFirstPosition(++source, letter);
}
上面的代码也改变了第二个参数的类型,但主要是受几个注释的启发编写的(特别感谢Yuli和Dmitri)

您可以使用以下功能:

int main()
{
    char *s = "Hello";
    char *p = recFirstPosition(s, 'l');
    if (p != NULL) {
        int index = p - s;
        printf("%s[%d] = %c\n", s, index, *p);
    }  
    return 0;
}

以下是我认为可行的方法。请测试它更多,因为我没有足够的时间来使用它 编辑:返回上一次出现的位置,但应提供足够的信息供您使用。
Edit2:更新了函数,因此现在它适用于

#include <stdio.h>

char *recFirstPosition(const char *s, int c, char *find){
    if(s==NULL) return NULL;
    if(*s == '\0') return (c == '\0') ? (char*)s : find;
    if(*s == c) return (char*) s;
    return recFirstPosition(s + 1, c, *s == c ? (char*)s : find);
}


int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=recFirstPosition(str,'s', NULL);
  // Uncomment the while loop to get all instances positions  
  //while (pch!=NULL)
  //{
    printf ("found at %d\n",pch-str+1);
  //  pch=recFirstPosition(pch+1,'s',NULL);

  //}
  return 0;
}
#包括
char*recFirstPosition(常量char*s、int c、char*find){
如果(s==NULL)返回NULL;
如果(*s=='\0')返回(c=='\0')?(char*)s:find;
if(*s==c)返回(char*)s;
返回recFirstPosition(s+1,c,*s==c?(char*)s:find);
}
int main()
{
char str[]=“这是一个示例字符串”;
char*pch;
printf(“在\%s\”..\n,str中查找's'字符);
pch=recFirstPosition(str,'s',NULL);
//取消对while循环的注释以获取所有实例的位置
//while(pch!=NULL)
//{
printf(“位于%d\n”,pch str+1);
//pch=recFirstPosition(pch+1,'s',NULL);
//}
返回0;
}
输出
正在“这是一个示例字符串”中查找“s”字符。。。
在4点被发现

不幸的是,您需要索引来实现此功能,或者需要某种计数机制。这两个函数都不会为所有控制路径返回值。第一步是确定您是否希望函数返回字符、指针或索引(int).“我想编写代码,但它不会返回我想要的内容”。这不是你的代码,是吗?如上所述,如果要返回索引位置,函数类型不能是
char*
测试时,它会打印一些数字,但我不认为这个数字是正确的位置:/n这是答案。她还可以返回
NULL
而不是0,这只是为了更具语义意义(虽然是0做的)@AnnaSobolewska在这个函数的返回值上使用printf(“%s”),您会看到它从
字母中打印出
字母,我需要打印这封信的确切位置,对吗?所以我需要一个整数值作为结果,对吗?不是整根绳子吗/那么,当
中的第一个字符不是
字母
'\0'
时,该代码返回什么?刚刚再次测试,看起来函数返回最后一次出现的位置。由于这是一个任务,我将把它留给您来解决。提示:您只需要修改recFirstPosition,以获得第一次出现的位置。。。还要注意的是,我在职位上加了1,所以它从1开始。。。