您的位置:首页 > 编程语言 > C语言/C++

c语言程序设计进阶week3:删除字符串中的子串(字符串与指针的完美结合)

2017-03-14 11:30 459 查看


题目来源自mooc:C语言程序设计进阶,仅供个人学习参考使用

#include <stdio.h>
#include <string.h>

int main(){
char s1[85], s2[85];
char *p;
char temp[85];

scanf("%s",&s1);
scanf("%s",&s2);

while((p=strstr(s1,s2)) != NULL) { //在s1和s2字符中找不到相同元素时停止,strstr字符串中招字符串
*p = '\0';//把s1自与s2相等的头处改为\0,截断
strcpy(temp , p+strlen(s2));//把p向后延到s1与s2相等的那段末尾处拷贝到 strcpy拷贝字符串
strcat(s1, temp );//把temp 接在s1后面
}

puts(s1);

return 0;

}

这一题很有意思
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: