1. 程式人生 > >c語言程式設計進階week3:刪除字串中的子串(字串與指標的完美結合)

c語言程式設計進階week3:刪除字串中的子串(字串與指標的完美結合)

題目來源自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;
	 
	 
	  
}

這一題很有意思