C语言Matrix编程题——[Pointers and C-String]D. Liang 7.12 Occurrences of each letter in a string

[Pointers and C-String]D. Liang 7.12 Occurrences of each letter in a string

Description:

Write a function that counts the occurrences of each letter in a string using the following header:

int * countLetters(const char * const s)

The function returns the counts as an array of 26 elements.
For example, after invoking

int counts[] = countLetters("ABcaB")

counts[0] is 2, counts[1] is 2, and counts[2] is 1.
Write another function to pass the count array in a parameter as follows:

void countLetters(const char * const s, int * counts, int size)

where size is the size of the counts array. In this case, it is 26.

Hint:

Don’t submit the main() function

Programme:

//Date:2020/5/18
//Kamenrider Justice
int * countLetters(const char * const s)//只用写一个函数,题目只是告诉我们还有另一种方法
{
   int length,* count,i;
   count=(int*)malloc(26*sizeof(int));//分配内存
   for(i=0;i<26;i++)//初始化
   {
      count[i]=0;
   }
   length=strlen(s);
   for(i=0;i<length;i++)//区分大小写
   {
      if(islower(s[i]))
      {
         count[s[i]-'a']++;
      }
      if(isupper(s[i]))
      {
         count[s[i]-'A']++;
      }
  
   }
   return count;
}

Redis 面试精讲

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值