实现代码如下:
#include <stdio.h>#include <stdlib.h>
int main()
{
char S[100],T[20]
char ch1,ch2
ch1=getchar()
int i=0
while(ch1!='\n')
{
S[i]=ch1
i++
ch1=getchar()
}
printf("请输入要筛选的字符串:\n")
ch2=getchar()
int j=0
while(ch2!='\n')
{
T[j]=ch2
j++
ch2=getchar()
世顷答}
int m,n//m为S的下标,n为T的下标
搜慧 m=0
n=0
int num=0//num用于记录选定单词出现的次数
while(m<=i&&n<=j)
{
if(S[m]==T[n])
{
m++
n++
}
else
{
m=m-n+1
n=0
}
if(n==j)
{
乎键 num++
}
}
if(m==i+1)
{
printf("出现的次数是%d",num)
}
}
代码二:
int strstr_cnt(const char *string ,const char *substring){
int i,j,k,count=0
for(i=0string[i]i++)
for(j=i,k=0string[j]==substring[k]j++,k++)
if(!substring[k+1])
count++
return(count)
}
#include <stdio.h>#include <stdlib.h>
#include <time.h>
#define N 20
void Sort(int a[],int n) {
int i,j,k,t
for(i = 0 i < n - 1 ++i) {
k = i
for(j = i + 1 j < n ++j) {
if(a[k] > a[j]) k = j
}
if(i != k) {
t = a[k]
a[k] = a[i]
a[i] = t
}
}
}
int Find(int a[],int n,int x) {
int low = 0,high = n - 升埋和1,mid
while(low <= high) {
mid 液枝= (low + high)/2
if(x == a[mid]) return mid
else if(x > a[mid]) low = mid + 1
else high = mid - 1
}
return -1
}
void Show(int a[],int n) {
int i
for(i = 0 i < n ++i) {
printf("%d ",a[i])
}
printf("\n")
}
int main() {
int a[20],i,x,res
srand((unsigned)time(NULL))
for(i = 0 i < N 吵盯++i)
a[i] = rand()%N // 每个数都在1 -- 100之间
Show(a,N)
Sort(a,N)
Show(a,N)
x = rand()%100 + 1
res = Find(a,N,x)
if(res >= 0) printf("数值%d的下标为%d。\n",a[res],res)
else printf("数列中没有找到数值%d。\n",x)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)