更多"编写函数findStr( ),该函数统计一个长度为2的子字符串在另一个"的相关试题:
[简答题]编写一个函数findStr( ),该函数统计一个长度为2的字符串在另一个字符串中出现的次数。例如,假定输入的字符串为"asd asasdfg asd as zx67 asd mklo",子字符串为"as",函数返回值是6。
函数ReadWrite( )的功能是实现从文件in68.dat中读取两个字符串,并调用函数findStr( ),最后把结果输出到文件out68.dat中。
注意:部分源程序已给出。
请勿改动主函数main( )和其他函数中的任何内容,仅在函数findStr( )的花括号中填入你所编写的若干语句。
[试题程序]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void ReadWrite( );
int findStr(char * str,char * substr)
void main( )
char str[81],substr[3];
int n;
system("CLS");
printf("输入原字符串:");
gets(str);
printf("输入子字符串:");
gets(substr);
puts(str);
puts(substr);
n=findStr(str,substr);
printf("n=% d/n",n);
ReadWrite( );
void meadWrite( )
char ch,str[81],substr[3];
int n,len,i=0;
FILE * rf, * wf;
rf=fopen("in68.dat","r");
wf=fopen("out68.dat","w");
while(i<5)
fgets(str,80,rf);
fgets(substr,10,r
[简答题]编写一个函数find Str( ),该函数统计一个长度为2的字符串在另一个字符串中出现的次数。例如,假定输入的字符串为"asd asasdfg asd as zx67 asd mklo",子字符串为“as”,函数返回值是6。
函数ReadWrite( )的功能是实现从文件in40.dat中读取两个字符串,并调用函数findStr( ),最后把结果输出到文件out40.dat中。
注意:部分源程序已给出。
请勿改动主函数main( )和其他函数中的任何内容,仅在函数findStr( )的花括号中填入你所编写的若干语句。
试题程序:
#include<stdio.h>
#include<string.h>
#include<conio.h>
int findStr(char *str,char *substr)
main( )
char str[81],substr[3];
int n;
clrscr( );
printf ("输入原字符串:");
gets(str);
printf("输入子字符串:");
gets(substr);
puts(str);
puts(substr);
n=findstr(str,substr);
printf("n=%d/n",n);
ReadWrite( );
ReadWrite( )
char ch,str[81],substr[3];
int n, len, i=0;
FILE *rf,*wf;
rf=fopen ("in40.dat","r");
wf=fopen ("out40.dar","w");
while (i<5)
fgets(str,80,rf);
fgets(substr,10,rf);
le
[简答题]编写一个函数,该函数可以统计一个长度为2的字符串在另一个字符串中出现的次数。例如,假定输入的字符串为asd asasdfg asd as zx67 asd mklo,子字符串为as,则应当输出6。
注意:部分源程序给出如下。
请勿改动主函数main和具他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <conio.h>
#include <stdio.h>
#include <string.h>
int fun(char *str, char *substr)
main ( )
char str[81],substr[3];
int n;
clrscr ( );
printf ("输入主字符串 ");
gets (str);
printf ("输入子字符串");
gets (substr);
puts (str);
puts (substr);
n=fun (shr, substr);
printf("n=%d/n ",n);
[简答题]编写一个函数,用该函数可以统计一个长度为3的字符串在另一个字符串中出现的次数。例如,假定输入字符串“the abcthe they have theren”,子字符串为“the”,则应输出4。
注意:部分源程序在文件PROC2.CPP中。
请勿改动主函数和其他函数中的任何内容,仅在fun( )的花括号中填入编写的若干语句。
部分源程序如下:
//PROC2.CPP
#include <iostream>
using namespace std;
#define MAX 100
int fun(char *str,char *substr);
int main( )
char str[MAX],substr[3];
int n;
cout<<"Please Input the source String/n";
cin>>str;
cout<<"Please Input the subString/n";
cin>>substr;
n=fun(str, substr);
cout<<"The counter is: "<<n<<end1;
return 0;
int fun(char *str,char *substr)
//******
[填空题]请补充函数fun( ),该函数可以统计一个长度为n的字符串在另一个字符串中出现的次数。例如,假定输入的字符串为: asd ascasdfg asd as asd mlosd,子字符串为asd,则应输出4。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun( )的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdio.h>
#include<string.h>
#include<conio.h>
int fun(char *str,char *substr)
{
int n;
char *p,*r;
【1】 ;
while(*str)
{
p=str;
r=substr;
while(*r)
if( 【2】 )
{
r++;
p++;
}
else
break;
if( 【3】 )
n++;
str++;
}
return n;
}
main( )
{
char str[81],substr[3];
int n;
clrscr( );
printf("输入主字符串: ");
gets(str);
printf("输入子字符串:");
gets(substr);
puts(str);
puts(substr);
n=fun(str,substr);
printf("n=%d/n",n);
}
[简答题]请编写一个函数int stringLen(char*ps),该函数能计算出字符串ps的长度,函数返回值就是字符串的长度(不包括字符串结束标识号’/0’)。本题要求:用指针方式及循环来实现该函数。
注意;部分源程序已存在考生文件夹下的文件PROC6,cpp中。
请勿修改主函数和其他函数中的任何内容,仅在函数stringLen( )的花括号中填写若干语句。
文件PROC6.cpp的内容如下:
//PROC6.cpp
#include<iostream>
using namespace std;
int stringLen(char *);
int main( )
char str[100],*p;
cout<<"Input your string please!/n";
cin>>str;
p=str;
cout<<"The lenth of your string is "<<stringLen(p)<<end1;
return 0;
int stringLen(char *ps)
// * * * * *
[简答题]编写一个函数findStr( ),该函数的功能是:统计一个以单词组成的字符串中(各单词之间以空格隔开)所含指定长度单词的个数。例如,输入字符串为"you are very good you",指定要查找的单词的长度为3,则函数返回值是3。
函数readWriteData( )的功能是从IN.DAT中读取字符串和子字符串,并把统计结果输出到屏幕和文件OUT.DAT中。
注意:部分源程序已给出。
请勿改动主函数main( )和函数WriteData(int n)中的内容。
试题程序:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void readWriteData( );
int findStr(char*str,int find_len)
void main( )
char str[81];
int find_len;
int n;
system("CLS");
printf("input the strings:");
gets(str);
printf("input the length:");
seanf("%d",&find_len);
puts(str);
printf("length=%d",find_len);
n=findStr(str,find_len);
printf("n=%d",n);
readWriteData( );
void readWriteData( )
char str[81],substr[11];
int n,len,i=0;
FILE*rf,*wf;
rf=fopen("IN.DAT","r");
wf=fopen("OUT.DAT","W");
while(i<3)
fgets(str,80,rf);
fgets(substr,10,rf)
[简答题]请编写函数fun( ),该函数的功能是:统计各年龄段的人数。N个年龄通过调用随机函数获得,并放在主函数的age数组中。要求函数把0至9岁年龄段的人数在d[0]中,把10至19岁年龄段的人数放在d[1]中,把20至29岁年龄段的人数放在 d[2]中,依次类推,把100岁(含100)以上年龄的人数都放在d[10]中。结果在主函数中输出。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <stdio. h>
# define N 50
# define M 11
void fun(int *a, int *b)
{
}
double rnd( )
{
static t=29, c=217,m=1024, r=0;
r= (r*t+c)%m;
return ((double) r/m);
}
main ( )
{
int age[N], i,d[M];
for(i=0; i<N; i++)
age [i]= (int) (i15*rnd ( ));
/*产生一个随机的年龄数组*/
printf ("The original data : /n");
for(i=0; i<N; i++)
printf((i+l)%10= =07 "%4d/n":"%4d",
age[i]); /*每行输出10个数* /
printf ("/n/n");
fun (age, d);
for(i=0; i<l0; i++)
printf("%4d---%4d :%4d/n", i*l0,
i*10+9, d[i] );
printf("Over 100 : %4dkn",d[10]).;
}
[简答题]请编写一个函数fun( ),其功能是比较两个字符串的长度(不得调用C语言提供的求字符串长度的函数),函数返回较长的字符串。若两个字符串长度相同,则返回第一个字符串。
例如,输入beijing<CR>shanghai<CR>(<CR>为回车键),函数将返回shanghai。
#include<stdio.h>
char*fun(char*s,char*t)
main( )
char a[20],b[20];
void NONO( );
printf("Input 1th string:");
gets(a);
printf("Input 2th string:");
gets(b);
printf("%s/n",fun(a,b));
[简答题]请编写函数fun( ),该函数的功能是:统计一行字符串中单词的个数,作为函数值返回。一行字符串在主函数中输入,规定所有单词由小写字母组成,单词之间有若干个空格隔开,一行的开始没有空格。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include<string. h>
# include<stdio, h>
#define N 80
int fun (char *s)
main ( )
char line [N];
int num=0;
printf ("Enter a string: /n ");
gets (line);
num=fun (line);
printf ("The number of word is: %d/n/n ",
num);