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

    xiaoxiao2021-03-25  70

    题目来源自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; } 这一题很有意思

    转载请注明原文地址: https://ju.6miu.com/read-50105.html

    最新回复(0)