山东科技大学OJ 1879 指针2

Problem D: 指针2

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 424  Solved: 122
[Submit][Status]

Description

这是关于指针的第二道题目,难度略有进阶

要求你写三个函数:

1.find_max(int *a, int len) 作用是返回长度为len的数组a中的最大值,参数必须是这个

2.get_data(int *a, int len)作用是往向a中输入len个数据

3.get_length(char *s) 返回字符串s中的字母的个数

大家尽量去理解指针的概念,不要一味的用s[1]这样的方式

Input

输入有三行

第一行一个数字,N,代表要输入的数据的个数

第二行有N个数字,代表输入的数据

第三行为一个字符串

Output

输出由主函数控制

Sample Input

5 1 2 3 4 5 abcde

Sample Output

The max value of this array is 5 The length of the letter in the string is 5

HINT

Append Code

int main() 
{
    int *a;
    a = (int *) malloc(sizeof(int) * 20);
    int len;
    scanf("%d", &len);
    get_data(a, len);
    int max = find_max(a, len);
    printf("The max value of this array is %d\n", max);
    free(a);
 
    char s[25];
    scanf("%s", s);
    int length = get_length(s);
    printf("The length of the letter in the string is %d", length);
    return 0;
}

Code

#include<stdio.h>
#include<stdlib.h>
int find_max(int *a, int len) 
{
    int max = *a;
    for(int i=1;i<len;i++)
    {
        if(max<*(a+i)) max = *(a+i);
    }
    return max;
}
void get_data(int *a, int len)
{
    for(int i=0;i<len;i++)
    {
        scanf("%d",a+i);
    }
}
int get_length(char *s)
{
    char *p = s;
    int count=0;
    for(int i=0;*(p+i)!='\0';i++)
    {
        if((*(p+i)>='a' &&*(p+i)<='z') || (*(p+i)>='A' &&*(p+i)<='Z')) count++;
    }
    return count;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值