C 練習(xí)實例96
題目:計算字符串中子串出現(xiàn)的次數(shù) 。
程序分析:無。
程序源代碼:
// Created by www.o2fo.com on 15/11/9. // Copyright © 2015年 W3Cschool教程. All rights reserved. // #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { int i,j,k,TLen,PLen,count=0; char T[50],P[10]; printf("請輸入兩個字符串,以回車隔開,母串在前,子串在后:\n"); gets(T); gets(P); TLen=strlen(T); PLen=strlen(P); for(i=0;i<=TLen-PLen;i++) { for(j=0,k=i;j<PLen&&P[j]==T[k];j++,k++) ; if(j==PLen)count++; } printf("%d\n",count); system("pause"); return 0; }
以上實例運行輸出結(jié)果為:
請輸入兩個字符串,以回車隔開,母串在前,子串在后: abca a 2
更多建議: