69,371
社区成员
发帖
与我相关
我的任务
分享
#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;
}