在形参s所指字符串数组中查找与形参t所指字符串相同的串,找到后返回该串在字符串数组中的位置(下标值),未找到则返回-1.ss所指字符串数组中共有N个内容不同的字符串,且串长小于M。
#include <stdio.h>
#include <string.h>
#define N 5
#define M 8
int fun(char (*ss)[M], char *t)
{
int i;
for (i = 0; i < N; i++)
if (strcmp(ss[i], t) == 0)
return i;
return -1;
}
int main()
{
char ch[N][M] = {"if", "while", "switch", "int", "for"}, t[M];
int n, i;
printf("The original string:\n");
for (i = 0; i < N; i++)
puts(ch[i]);
printf("\n");
printf("Enter a string for search: ");
gets(t);
n = fun(ch, t);
if (n == -1)
printf("\nDon't found!\n");
else
printf("\nThe position is %d .\n", n);
}
程序结果: