4.1编写函数strindex(s,t)它返回字符串t在s中最右边出现的位置。如果s中不包含t,则返回-1。

4.1编写函数strindex(s,t)它返回字符串t在s中最右边出现的位置。如果s中不包含t,则返回-1。

官方答案多一个变量 j , 用来记录i , j = i;多判断了t[k] == '\0'   结果得到的一直是-1

 

#include "stdafx.h"

int strindex(char s[], char t[]); //函数声明

int strindex(char s[], char t[])
{
 int i, k, pos; // 参数说明 i 记录S元素的位置 k 记录T元素的位置 pos记录s[i] = s[k]且在最右边时的位置
 pos = -1;
 for (i = 0; s[i] != '\0';i++)
 {
  for (k = 0; t[k] != '\0' && t[k] == s[i];k++)
  {
   pos = i;
  }
 }
 return pos;
}

int _tmain(int argc, _TCHAR* argv[])
{
 char s[10] = {"cuouclc"};
 char t[5] = {"c"};
 printf("%d",strindex(s,t));
 getchar();
 return 0;
}

posted @ 2023-07-31 10:09  Maguyusi  阅读(46)  评论(0)    收藏  举报  来源