您的位置:首页 > 大数据 > 人工智能

nankai100_4

2015-11-25 12:18 393 查看
/// @file exam_x_x.cpp
/// @brief nankai100_4
/**
4: 第4题 请编写一个函数void fun(char *tt,int pp[]),
统计在tt字符中"a"到"z"26各字母各自出现的次数,并依次放在pp所指的数组中。 
*/

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <crtdbg.h>
#include <conio.h>

/// 来段歌词吧~
char g_cMsg[] = {
        "Oscar The Angel (In the Style of Randy Travis) {Karaoke Lead Vocal Version} - The Karaoke Channel"
        "Oscar was an angel and he used to walk the streets"
        "Shoutin′ out some prophecy at everyone he′d meet"
        "He was a local fixture like a cop out on a beat"
        "Folks said he′d been shell-shocked long ago"
        "And more than that, no one seemed to know"
        "Oscar was a walker, at least four miles twice a day"
        "The entire length of main street he′d be shoutin′ all the way"
        "And I had no idea where he′d heard the things, he′d say"
        "But he was not your normal voice of doom"
        "It was a happy song, sung slightly out of tune"
        "And he′d say, “Everyone will die and go to heaven"
        "And we will all be angels some day"
        "What you are in this world don′t count for nothin′"
        "′Cause we are only children, we′re just lost along the way"
        "But we will all be angels some day“"
        "Well I worked at the Rial to, I sold tickets at the door"
        "And Oscar he′d come by most ev′ryday ′bout half past four"
        "And he′d pay to see some movie that he′d seen ten times before"
        "But mostly we′d just let him in for free"
        "And he′d watch five minutes, and he′d come and talk to me"
        "And he′d say, “Everyone will die and go to heaven"
        "And we will all be angels some day"
        "What you are in this world don′t count for nothin′"
        "′Cause we are only children, we′re just lost along the way"
        "But we will all be angels some day“"
        "Well it′s going on ten years now since I left my home town"
        "And I went back last summer; for a week I hung around"
        "I looked out for Oscar, he was nowhere to be found"
        "Someone said they finally had to commit him"
        "And he died, before they had time to forget him"
        "Now I′m not about to argue, Oscar′s train had jumped the tracks"
        "But I′ll bet my last dollar on a plain and simple fact"
        "Oscar never said a word about me behind my back"
        "And the way that I was raised to understand"
        "That bone, it makes him the better man"
        "And he′d say, “Everyone will die and go to heaven"
        "And we will all be angels some day"
        "What you are in this world don′t count for nothin′"
        "′Cause we are only children, we′re just lost along the way"
        "We will all be angels some day“"
        "Ya, we will all be angels someday"
};

void fun(char *tt, int pp[]);
void print_int_array(char* pcTip, int iAry[], int iSizeAry);

int main(int argc, char *argv[ ], char *envp[ ])
{
    int iAry[26] = {0};

    fun(g_cMsg, iAry);
    print_int_array("iAry[] =", iAry, sizeof(iAry) / sizeof(int));

    /**
    iAry[] =
    
    145 23 30 86 203 23 27 80
    66 5 17 100 44 113 129 10
    0 69 88 122 42 13 52 1
    45 0
    */
    
    printf("END, press any key to quit\n");
    getchar();
    
    return 0;
}

void fun(char *tt, int pp[])
{
    char cTmp = '\0';
    size_t nIndex = 0;
    size_t nLen = 0;

    _ASSERT(tt);
    _ASSERT(pp);

//     4: 第4题 请编写一个函数void fun(char *tt,int pp[]),
//         统计在tt字符中"a"到"z"26各字母各自出现的次数,并依次放在pp所指的数组中。 

    nLen = strlen(tt);
    for (nIndex = 0; nIndex < nLen; nIndex++)
    {
        cTmp = tt[nIndex];
        if ((cTmp < 'a') || (cTmp > 'z'))
        {
            continue;
        }

        pp[cTmp - 'a']++;
    }
}

void print_int_array(char* pcTip, int iAry[], int iSizeAry)
{
    int i = 0;
    int iPos = 0;
    
    if (NULL != pcTip)
    {
        printf("%s\n", pcTip);
    }
    
    for (i = 0; i < iSizeAry; i++)
    {
        if (0 == iPos++ % 8)
        {
            printf("\n");
        }
        
        printf("%d ", iAry[i]);
    }
    
    printf("\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: