1题
编写函数返回子串在字符串中出现的次数
2题
请阅读下面的程序,在横线处填写正确的代码,该程序的功能是:求1-10的奇数和。
#include <stdio.h>
int main()
{
int x, s;
s=0;
for (x=1; x<=10; _______)
{
}
printf("奇数和为:%d", s);
return 0;
}
1.
#include <stdio.h>
#include <string.h>
int find(char *str1, char *str2)
{
int i, j;
int str1len = strlen(str1), str2len = strlen(str2);
int count = 0;
for (i = 0; i < str1len - str2len + 1; i++)
{
for (j = 0; j < str2len; j++)
{
if (str2[j] != str1[i + j])
break;
}
if (j == str2len)
count++;
}
return count;
}
int main()
{
char a[200], b[200], *g;
int c = 0;
printf("请输入主串:");
gets(a);
printf("请输入子串:");
gets(b);
c = find(a, b);
printf("出现的次数:%d\n", c);
return 0;
}
2.
x ++
if(x%2!=0)
s+=x;
i += 2;
s += x;
第一题
int find(char*a){
char*b=(char*)malloc(strlen(a)+1);
scanf("%s",b);
int i,j,cnt=0;
for(i=0;i<strlen(a);i++){
if(a[i]==b[0]){
for(j=0;j<strlen(b);j++){
if(a[i+j]==b[j])
cnt++;
}
}
}
free(b);
return cnt;
}
第二题
#include <stdio.h>
int main()
{
int x, s;
s=0;
for (x=1; x<=10; x++)
{
if(x%2!=0)
s+=x;
}
printf("奇数和为:%d", s);
return 0;
}
//第二题有用的话采纳一下,谢谢,
#include <stdio.h>
int main()
{
int x, s;
s=0;
for (x=1; x<=10;)
{
s+=x;
x=x+2;
}
printf("奇数和为:%d", s);
return 0;
}
```c++
#include <stdio.h>
int main()
{
int x, s;
s=0;
for (x=1; x<=10; x++)
{
if(x%2!=0)
s+=x;
}
printf("奇数和为:%d", s);
return 0;
}
```