返回最后的字符串

题目描述

  findFinal函数的功能是求出s所指字符串中最后一次数显的t所指向的子字符串地址,通过函数值返回。

  在主函数中输入从此地址开始的字符串,若未找到,则函数返回NULL.

  例如,输入"pmbhahahahpmbyyy",t中输入"pmb",则程序返回"pmbyyy".

输入描述

  输入2行字符串分别为S和T

输出描述

  输出T在S中所指向的字符串地址

输入样例

pmbhahahahpmbyyy
pmb

输出样例

pmbyyy

测试代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define N 1024
 5 
 6 char *finalFind(char *s, char *t)
 7 {
 8     char *temp1, *temp2, *a;
 9     a = NULL;
10     while (*s)
11     {
12         temp1 = s;
13         temp2 = t;
14         while (*temp2)
15         {
16             if (*temp2 == *temp1)
17             {
18                 temp2++;
19                 temp1++;
20             }
21             else
22                 break;
23         }
24         if (*temp2 == '\0')
25             a = s;
26         s++;
27     }
28     return a;
29 }
30 
31 int main()
32 {
33     char s[N], t[N], *p;
34     scanf("%s", s);
35     scanf("%s", t);
36     p = finalFind(s, t);
37     if (p)
38         printf("%s\n", p);
39     else
40         printf("not found!\n");
41     return 0;
42 }

 

posted @ 2016-05-31 16:49  新生代黑马  阅读(287)  评论(0)    收藏  举报