给定两个字符串,X,Y 问Y中有多少个字符有在X中出现

给定两个字符串,X,Y 问Y中有多少个字符有在X中出现

  给定两个字符串:X,Y。问Y中有多少个字符是在X中出现。限定X字符串中每个字符都不一样。

  例:

  input : X="aA"    Y="aAAbbbb"

  output: 3

  input  X="z"   Y="ZZ"

  output:0

#include <stdio.h>
#include <string.h>
int count_characters(char *x, char *y);

int count_characters(char *x, char *y) {
  int count = 0;                                      //出现次数统计
  for (int i = 0; i < strlen(y); i++) {        
    for (int k = 0; k < strlen(x); k++) {     
      if (y[i] == x[k]) {
        count++;
      }
    }
  }
  return count;
}

int main(void) {
  char *x1 = "aA";
  char *y1 = "aAAbbbb";
  char *x2 = "z";
  char *y2 = "ZZ";
  printf("%s中有%d个字符在%s中出现\n", y1, count_characters(x1, y1), x1);
  printf("%s中有%d个字符在%s中出现\n", y2, count_characters(x2, y2), x2);
  return 0;
}

1.png

点击这里复制本文地址 欢迎来到大黄鸡源码分享网
qrcode

大黄鸡源码编程网 © All Rights Reserved.  
网站备案号:闽ICP备18012015号-4
Powered by Z-BlogPHP
联系我们| 关于我们| 广告联系| 网站管理