-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlettercount.cpp
81 lines (72 loc) · 1.32 KB
/
lettercount.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// main.cpp
// practice
//
// Created by Cindy on 5/7/17.
// Copyright © 2017 Ying. All rights reserved.
//
/*
* Question: Give data like aaabbccdd
* Return: a3b2c2d2
*/
#include <iostream>
#include "string.h"
using namespace std;
void count_char(char* data, int length){
//int count[] to count the numbers of one letter
int count[26];
int c = 0;
int k = 0;
int j = 0;
int len = 0;
int index = 0;
//initilize every element in the array is 0
for(int i = 0; i<26;i++)
{
count[i]=0;
}
//loop the data to count
while(data[c]!= '\0')
{
index = data[c] -'a';
count[index]++;
c++;
/*cout<<"index: "<<index<<endl;
cout<<"data[i]: "<<data[i]<<endl;*/
}
//len(gth) of new char array
while (count[j]!=0)
{
len++;
j++;
}
//cout the original one if not shorter
int newlen = 2*len+1;
if (length<newlen)
{
for(int i = 0;i<length-1;i++)
{
cout<<data[i];
}
cout<<endl;
}
else
{
while(k<26)
{
if (count[k]!=0)
{
cout<<char(k+'a');
cout<<count[k];
}
k++;
}
cout<<endl;
}
}
int main(int argc, const char * argv[]) {
char data[11]="aaabbccadd";
data[10]='\0';
count_char(data, 11);
return 0;
}