有两个字符串s,t判断t是否是s的子串,如果是输出子串的位置,如果不是输出0,再删除子串并且输出删除后的串。

weixin_44990738 2019-05-09 02:24:41
有两个字符串s,t判断t是否是s的子串,如果是输出子串的位置,如果不是输出0,再删除子串并且输出删除后的串。
...全文
1295 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_44990738 2019-05-09
  • 打赏
  • 举报
回复
我刚才试了一下,可以,谢谢了大神
weixin_44990738 2019-05-09
  • 打赏
  • 举报
回复
我刚才试了一下,可以,谢谢了大神
weixin_44990738 2019-05-09
  • 打赏
  • 举报
回复
我刚才试了一下,可以,谢谢了大神
自信男孩 2019-05-09
  • 打赏
  • 举报
回复
引用 3 楼 weixin_44990738 的回复:
运行了没有啊,大神,回复我

啥意思?你自己没试试吗?
weixin_44990738 2019-05-09
  • 打赏
  • 举报
回复
运行了没有啊,大神,回复我
自信男孩 2019-05-09
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

int find_pos_str(const char *str, const char *substr);
char *del_substr(char *str, int pos, int sub_len);

int main(void)
{
char str[64], substr[32];
int pos;

scanf("%s", str);
scanf("%s", substr);

pos = find_pos_str(str, substr);
printf("%d\n", pos);
if (pos >= 0)
printf("%s\n", del_substr(str, pos, strlen(substr)));

return 0;
}


int find_pos_str(const char *str, const char *substr)
{
const char *tmp_str = str;
int len = strlen(substr), pos = 0;

while (*tmp_str) {
if (strncmp(tmp_str, substr, len) == 0)
break;
pos++;
tmp_str++;
}

if (!*tmp_str)
return -1;

return pos;
}

//pos: the position of substr
char *del_substr(char *str, int pos, int sub_len)
{
int len = strlen(str);

if (pos + sub_len >= len ) {
*(str + pos) = 0;
return str;
}

strcpy(str + pos, str + pos + sub_len);

return str;
}


供参考~

weixin_44990738 2019-05-09
  • 打赏
  • 举报
回复
求解答啊,急,急,急,大神们

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧