一个笔试题

华为面试题目

char *str = "AbcABca";
写出一个函数,查找出每个字符的个数,区分大小写,要求时间复杂度是n(提示用ASCⅡ码)
我是怎么都没想出来,望高手赐教

 

#include <iostream>
using namespace std;
#define MAX_PATH 256
int main()
{

    
int arr[MAX_PATH]={0};
    
char *s="AbcABca";
    
for(int i=0;i<strlen(s);++i)
        arr[s[i]]
++;

    
for(int i=0;i<256;++i)
    {
        
if(arr[i])
            cout
<<(char)i<<":"<<arr[i]<<" ";
    }
    cout
<<endl;
}
#include <stdio.h>

int main(int argc, char** argv)
{
    
char *str = "AbcABca";
    
int count[256= {0};

    
for (char *p=str; *p; p++)
    {
        count[
*p]++;
    }

    
// print
    for (int i=0; i<256; i++)
    {
        
if (count[i] > 0)
        {
            printf(
"The count of %c is: %d\n",i, count[i]);
        }
    }

    
return 0;
}
#include <iostream>
#include 
<cstring>
using namespace std;

void count(const char *str)
{
    
static int count[26*2];

    memset(count,
0,sizeof(count));

    
for(;*str!=0;++str)
    {
        
if('a'<=*str && *str<='z')
        {
            
++count[*str-'a'];
        }
        
else
        {
            
++count[*str-'A'+26];
        }
    }

    
//打印

    
for(int i=0;i<25;++i)
    {
        
if(count[i]!=0)
        {
            cout
<<(char)('a'+i)<<":"<<count[i]<<endl;
        }

        
if(count[26+i]!=0)
        {
            cout
<<(char)('A'+i)<<":"<<count[26+i]<<endl;
        }
    }
}

int main()
{
    
char buffer[100];
    cin
>>buffer;

    count(buffer);

    
return 0;
}
原文地址:https://www.cnblogs.com/feng801/p/1999867.html