编写一个函数 int count_chars(char const *str,char const *chars) 函数应该在第一个参数中进行查找, 并返回匹配第二个参数所包含的字符的数量。

来源:互联网 发布:网络的攻击与防范 编辑:程序博客网 时间:2024/05/01 23:57
/**************************************** *  File Name  : count_chars.c *  Creat Data : 2015.1.22*  Author     : ZY *****************************************/ /*编写一个函数int count_chars(char const *str,char const *chars)函数应该在第一个参数中进行查找,并返回匹配第二个参数所包含的字符的数量。*/#include <stdio.h>#include <assert.h>int count_chars(char const *str,char const *chars){int count = 0;char p[26] = {0};assert(str);assert(chars);while(*str){p[*str - 'a'] = *str;str++;}while(*chars){if(p[*chars - 'a'] != 0)//字符0是终止符'\0'{count++;}chars++;}return count;}



#include <stdio.h>#include <assert.h>#include <string.h>int count_chars(char const *str,char const *chars){int count = 0;int i,j;assert(str);assert(chars);for (i = 0;i < strlen(str);i++ ){for(j = 0;j < strlen(chars);j++){if( str[i] == chars[j]){count++;}}}return count;}



#include <stdio.h>#include <assert.h>int count_chars(char const *str,char const *chars){int count = 0;assert(str);assert(chars);while(*chars){char *p = str;while(*p){if( *p == *chars ){count++;break;}p++;}chars++;}return count;}




int main()  {    char *arr = "012345";  char *brr = "20354";  printf("%d\n",count_chars(arr,brr));  return 0;  }


0 0