C语言编程题:求指定字符在字符串中出现的次数并输出
例如:输入“thisisaprogram",当指定字符是”a“时,输出”theresultis:2"#include<string.h>#include<stdio.h>...
例如:输入“this is a program",当指定字符是”a“时,输出”the result is:2"
#include <string.h>
#include <stdio.h>
int fun(char str1[],char ch)
{
/***********begin***********/
/************end************/
}
void main()
{
void NONO( );//函数声明
char str1[80],ch;
int count;
printf("Enter str1:\n");
gets(str1);
printf("Enter ch:\n");
ch=getchar();
count=fun(str1,ch);
printf("Result is: %d\n",count);
NONO( );
}
void NONO( )
{FILE *fr,*fw;
int i, count;
char str[80], ch;
fr=fopen("D:\\exam\\03100210\\PROGIN.DAT","r");
fw=fopen("D:\\exam\\03100210\\PROGOUT.DAT","w");
for(i=1; i<=2; i++)
{ fscanf(fr,"%s",str);fgetc(fr);
ch=fgetc(fr);
count=fun(str,ch);
fprintf(fw,"Result is :##%d\n",count); }
fclose(fr);
fclose(fw);
} 展开
#include <string.h>
#include <stdio.h>
int fun(char str1[],char ch)
{
/***********begin***********/
/************end************/
}
void main()
{
void NONO( );//函数声明
char str1[80],ch;
int count;
printf("Enter str1:\n");
gets(str1);
printf("Enter ch:\n");
ch=getchar();
count=fun(str1,ch);
printf("Result is: %d\n",count);
NONO( );
}
void NONO( )
{FILE *fr,*fw;
int i, count;
char str[80], ch;
fr=fopen("D:\\exam\\03100210\\PROGIN.DAT","r");
fw=fopen("D:\\exam\\03100210\\PROGOUT.DAT","w");
for(i=1; i<=2; i++)
{ fscanf(fr,"%s",str);fgetc(fr);
ch=fgetc(fr);
count=fun(str,ch);
fprintf(fw,"Result is :##%d\n",count); }
fclose(fr);
fclose(fw);
} 展开
展开全部
#include <string.h>
#include <stdio.h>
int fun(char str1[],char ch)
{
/***********begin***********/
int i;
int sum=0;
for(i=0;i<strlen(str1);i++)
{
if(str1[i]==ch)
sum++;
}
return sum;
/************end************/
}
void main()
{
void NONO( );//函数声明
char str1[80],ch;
int count;
printf("Enter str1:\n");
gets(str1);
printf("Enter ch:\n");
ch=getchar();
count=fun(str1,ch);
printf("Result is: %d\n",count);
NONO( );
}
void NONO( )
{FILE *fr,*fw;
int i, count;
char str[80], ch;
fr=fopen("D:\\exam\\03100210\\PROGIN.DAT","r");
fw=fopen("D:\\exam\\03100210\\PROGOUT.DAT","w");
for(i=1; i<=2; i++)
{ fscanf(fr,"%s",str);fgetc(fr);
ch=fgetc(fr);
count=fun(str,ch);
fprintf(fw,"Result is :##%d\n",count); }
fclose(fr);
fclose(fw);
}
湖工大 彭 路
#include <stdio.h>
int fun(char str1[],char ch)
{
/***********begin***********/
int i;
int sum=0;
for(i=0;i<strlen(str1);i++)
{
if(str1[i]==ch)
sum++;
}
return sum;
/************end************/
}
void main()
{
void NONO( );//函数声明
char str1[80],ch;
int count;
printf("Enter str1:\n");
gets(str1);
printf("Enter ch:\n");
ch=getchar();
count=fun(str1,ch);
printf("Result is: %d\n",count);
NONO( );
}
void NONO( )
{FILE *fr,*fw;
int i, count;
char str[80], ch;
fr=fopen("D:\\exam\\03100210\\PROGIN.DAT","r");
fw=fopen("D:\\exam\\03100210\\PROGOUT.DAT","w");
for(i=1; i<=2; i++)
{ fscanf(fr,"%s",str);fgetc(fr);
ch=fgetc(fr);
count=fun(str,ch);
fprintf(fw,"Result is :##%d\n",count); }
fclose(fr);
fclose(fw);
}
湖工大 彭 路
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询