讲解对象:数组法数字符
作者:融水公子 rsgz
思路 int fun(char *ss,char c){}
具体代码:
#include<stdio.h>
#include<stdlib.h>
#include<string>
#define M 81
int fun(char *str, char c) {
int i, count = 0;
for (i = 0; str[i]; i++) {
if (str[i] == c) {
count++;
}
}
return count;
}
int main() {
char a[M], ch;
FILE *out;
printf("Please enter a string:\n");
gets_s(a); printf("Please enter a char:\n");//从键盘获得数据存入a数组
ch = getchar();//获得字符方法
printf("the number char is:%d\n", fun(a, ch));
out = fopen("d:\\out.txt", "w");//fopen使用之前项目属性里面 必须要加上预处理器 _CRT_SECURE_NO_WARNINGS;
strcpy(a, "the number of the char is:");
fprintf(out, "%d", fun(a, ' '));
fclose(out);//将文件指针变量与指定文件之间的联系取消
system("pause");
return 0;
}
联系客服