简单不出门赚钱的工作:键盘输入两个字符串Tianjin University,VisualC Language在一个程序中分别统计字符串字母a,i出现次数,输

来源:百度文库 编辑:中科新闻网 时间:2024/05/10 07:59:10
考试呢,快回答啊,谢谢了
用C++

// zd_77.cpp : Defines the entry point for the console application.
//

#include <stdio.h>

void my_count(int &a,int &b)
{
char buffer[81];
int i, ch;
/* Read in single line from "stdin": */
for( i = 0; (i < 80) && ((ch = getchar()) != EOF)
&& (ch != '\n'); i++ )
{
buffer[i] = (char)ch;
if(buffer[i]=='a')
a++;
else if(buffer[i]=='i')
b++;
}
}

void main( void )
{

int num_a=0,num_i=0;
printf( "Enter str1: " );
my_count(num_a,num_i);
printf("the count of a:%d\n",num_a);
printf("the count of i:%d\n",num_i);
num_a=0,num_i=0;
printf( "Enter str2: " );
my_count(num_a,num_i);
printf("the count of a:%d\n",num_a);
printf("the count of i:%d\n",num_i);
}

运行结果:
Enter str1: fiadshfisd
the count of a:1
the count of i:2
Enter str2: ifdsjifhdsa
the count of a:1
the count of i:2
Press any key to continue