編寫程序,輸入兩個字符串string1和string2犁柜,檢查在string1中是否包含有string2洲鸠,
如果有,則輸出string2在string1中的起始位置馋缅;如果沒有扒腕,則顯示“NO”;如果string2在
string1中多次出現(xiàn)萤悴,則輸出在string1中出現(xiàn)的次數(shù)以及每次出現(xiàn)的起始位置瘾腰,例如:
string1="the day the month the year";
string2="the"
輸出結果應為:出現(xiàn)三次,起始位置分別是:0,8,18覆履。
又如:
string1="aaabacad"
string2="a"
輸出結果應為:出現(xiàn)五次蹋盆,起始位置分別是:0,1,2,4,6费薄。
輸入輸出格式要求:
輸入格式:string1回車string2回車
例如:
輸入:the day the month the year回車the回車
輸出:3times,0,8,18
輸入:aaabacad回車a回車
輸出:5times,0,1,2,4,6
輸入:aaabacad回車e回車
輸出:NO
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s[100], t[100];
int a[100];
gets_s(s,100);
gets_s(t,10);
int i = 0, j = 0,m=0,times=0;
while (s[i])
{
while (s[i + j] && t[j] && s[i + j] == t[j])
{
j++;
}
if (t[j] == '\0')
{
a[m] = i;
m++;
times++;
}
j = 0;
i++;
}
if (m == 0)
printf("NO");
else
{
printf("%dtimes", times);
for (int i = 0; i < m; i++) {
printf(",%d", a[i]);
}
}
system("pause");
return 0;
}