//計算字串中相同字元的個數
#include "stdio.h"
#include "string.h"
int count_str_same(char * p,char ch);
int main()
{
int same_ch;
char a[100];
char c;
printf("請輸入字串:");
gets(a);
printf("請輸入欲統計的重複字元:");
c = getchar();
same_ch = count_str_same(a,c);
printf("該字串中字元%c重複%d次\n",c,same_ch);
return 0;
}
int count_str_same(char * p,char ch)
char * q = p;
int m = 0;
while(* q != "\0")
if(ch == * q)
m++;
q++;
return m;
//計算字串中相同字元的個數
#include "stdio.h"
#include "string.h"
int count_str_same(char * p,char ch);
int main()
{
int same_ch;
char a[100];
char c;
printf("請輸入字串:");
gets(a);
printf("請輸入欲統計的重複字元:");
c = getchar();
same_ch = count_str_same(a,c);
printf("該字串中字元%c重複%d次\n",c,same_ch);
return 0;
}
int count_str_same(char * p,char ch)
{
char * q = p;
int m = 0;
while(* q != "\0")
{
if(ch == * q)
m++;
q++;
}
return m;
}