现在的位置: 首页 > 14 二级C语言 > 正文

2011春江苏省二级C上机真题C02 改错题

2011年09月06日 14 二级C语言 ⁄ 共 457字 ⁄ 字号 暂无评论

本试卷完成时间为70分钟,其中改错题(16分),很容易拿到手。

一、 改错题(16分)
【程序功能】
统计一个字符串中包含的字母个数并找出其中最长的字母串。所谓字母串是指一个连续的字母序列(不区分大小写),字母串之间用非字母字符分隔。
函数count的功能是统计p指向的字符串中包含的字母个数,并将找出的最长字母串存放在pmax指向的数组中,函数返回字母串的个数。
【测试数据与运行结果】
测试数据:you are teacher234too.
屏幕输出:a=you are teacher234too.
number is 4
max string is:teacher

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int count (char p[],char pmax[])
{
    int j=0,k,m=0;
    char temp[100];
    while(*p) {
        while((!isalpha(*p))&&*p) p++;
        k=0;
        if(*p!='\0') m++;
        while(isalpha(*p))
            temp[k++]=*p++;
        temp[k]= "\0";
        if(k<j) {
            j=k;
            pmax=temp;
        }
    }
    return m;
}
 
void main()
{
    char a[100]="you  are  teacher234too.",max[100];
    int i;
    i=count(a[],max[]);
    if(i==0)
        printf("a=%s N0 letter string!\n",a);
    else
        printf("a=%s\nnumber is %d\nmax string:%s\n",a,i,max);
}

抱歉!评论已关闭.