输入两个字符串,找出在第一个字符串中,有多少个第二个子串,输出个数,并输出删除全部第二个子串后的字符串。
输入:abcdcde cd
输出:2 abe
#include <iostream> #include <string> using namespace std; int delete_sub_str(const char *str,const char *sub_str,char *result) { const char *p,*q; char *t,*tmp; int n=0; int num=0;//相同子串个数 p=str; q=sub_str; t=result; n=strlen(q); tmp =new char[n+1]; memset(tmp,0,n+1); while (*p) { memcpy(tmp,p,n);//从p的初始位置开始拷贝n个字符到tmp if (strcmp(tmp,q)==0)//tmp=q { num++; memset(tmp,0,n+1); p=p+n; } else { *t = *p; p++; t++; memset(tmp,0,n+1); } } delete tmp; return num; } void main() { char s[100] = {'\0'}; int num = delete_sub_str("123abc123de1234fg1hi34j123k","123",s); cout<<num<<endl; cout<<s<<endl; }
import java.util.Scanner; public class deleteCuan { public static void main(String[] args) { Scanner s = new Scanner(System.in); String str = s.nextLine(); Scanner s1 = new Scanner(System.in); String str1 = s1.nextLine(); int count=0; int k = str.indexOf(str1); while(k!=-1){ count++; k=str.indexOf(str1,k+str1.length()); } System.out.println(count); System.out.println(str.replace(str1,"")); } }