鱼C论坛

 找回密码
 立即注册
查看: 1027|回复: 8

[已解决]C语言填空题

[复制链接]
发表于 2021-8-2 22:45:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
下面函数用于计算子串substr在母串str中第一次出现的位置,如果母串中不包含子串,则返回0值,例如,at("ver","university")返回的值为4,at("ty","string")返回的值为0.

int at(char *substr, char *str){
    int i,j,post;
    for(post=0;str[post]!=①;post++){
        i=0;j=②;
        while(substr[i]!='\0' && substr[i]==str[j]){
            i++;
            j++;
        }
        if(substr[i]=='\0')
           return ③;
    }
    return ④;
}


我觉得①填“\0”         ②填0         ③填0             ④填4

烦请各位帮我看看,填的对错与否,不对的地方还请指正,麻烦了
最佳答案
2021-8-2 23:20:34
全错
  1. #include <stdio.h>

  2. int at(char *substr, char *str){
  3.     int i, j, post;
  4.     for(post = 0; str[post] != '\0'; post++){
  5.         i = 0; j = post;
  6.         while(substr[i] != '\0' && substr[i] == str[j]){
  7.             i++;
  8.             j++;
  9.         }
  10.         if(substr[i] == '\0')
  11.             return post;
  12.     }
  13.     return 0;
  14. }

  15. int main(void) {
  16.     printf("%d\n", at("ver", "university"));
  17.     printf("%d\n", at("ty", "string"));
  18.     return 0;
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-2 23:17:04 | 显示全部楼层
你编译运行一下就知道对不对了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-2 23:18:40 | 显示全部楼层
自己写一写代码,运行一下,验证一下你的猜想,像这样

  1. #include <stdio.h>

  2. int at(char *substr, char *str){
  3.     int i, j, post;
  4.     for(post = 0; str[post] != "\0"; post++){
  5.         i = 0; j = 0;
  6.         while(substr[i] != '\0' && substr[i] == str[j]){
  7.             i++;
  8.             j++;
  9.         }
  10.         if(substr[i] == '\0')
  11.             return 0;
  12.     }
  13.     return 4;
  14. }

  15. int main(void) {
  16.     printf("%d\n", at("ver", "university"));
  17.     printf("%d\n", at("ty", "string"));
  18.     return 0;
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-2 23:20:34 | 显示全部楼层    本楼为最佳答案   
全错
  1. #include <stdio.h>

  2. int at(char *substr, char *str){
  3.     int i, j, post;
  4.     for(post = 0; str[post] != '\0'; post++){
  5.         i = 0; j = post;
  6.         while(substr[i] != '\0' && substr[i] == str[j]){
  7.             i++;
  8.             j++;
  9.         }
  10.         if(substr[i] == '\0')
  11.             return post;
  12.     }
  13.     return 0;
  14. }

  15. int main(void) {
  16.     printf("%d\n", at("ver", "university"));
  17.     printf("%d\n", at("ty", "string"));
  18.     return 0;
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-2 23:40:59 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>
  3. int at(char *substr, char *str);

  4. int at(char *substr, char *str){
  5.        
  6.     int i,j,post;
  7.     for(post=0;str[post]!='\0';post++) // 母串作为主循环,用母串的字符一个个去匹配子串
  8.         {  
  9.         i=0;j=post; // 记录母串当前的下标
  10.         while(substr[i]!='\0' && substr[i]==str[j]) //发现与子串第一个字符匹配时,进入字串循环匹配
  11.                 {
  12.                         //
  13.             i++;
  14.             j++;
  15.         }
  16.         if(substr[i]=='\0')//判断匹配是否成功,是则返回当前母串下标
  17.            return post;  
  18.     }
  19.     return 0; // 因为时母串作为主循环,当里面无返回,则表示没有匹配,所以返回0
  20. }
  21. int main()
  22. {
  23.         char *substr ="ty";
  24.         char *str = "stying";
  25.         int a;
  26.         a = at(substr,str);
  27.         if (a==0)
  28.                 printf("%s 不是 %s 的字串",substr,str);
  29.         else
  30.                 printf("%s 是 %s 的字串,出现在%d上",substr,str,a);
  31.         return 0;
  32. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-3 08:15:44 | 显示全部楼层
‘\0’
0
j-i+1
0
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-3 08:53:07 | 显示全部楼层

你好,我想问下,这里"ty", "string",ty和string不是有相同的t嘛,为啥返回的是0,而不是1?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-3 10:12:53 | 显示全部楼层
1163028233 发表于 2021-8-3 08:53
你好,我想问下,这里"ty", "string",ty和string不是有相同的t嘛,为啥返回的是0,而不是1?

这是匹配子串,要求全部匹配才行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-3 20:18:48 | 显示全部楼层
人造人 发表于 2021-8-3 10:12
这是匹配子串,要求全部匹配才行

原来如此,感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 05:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表