Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

11/1/2015

Fresher [0Yr - 1Yr Exp] :: powered by HackerRank

Fresher [0Yr - 1Yr Exp]

00:10:19

to test end

Count words
Given a paragraph P as a character array, write a function
int[] count_words(char[] para)
to return an array of length 26 whose values are like as below:

dup[0] = number of words that has alphabet A or a occurring


more than once.
.
.
dup[25] = number of words that has alphabet Z or z occurring
more than once.

Input Format:
Paragraph P as a character array

Output format:
An array of length 26 whose values printed as {., ., .}

Sample Input #1:


Again it is happening inside Aarons head.

Sample Output #1:


{2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Explanation #1:
Here,
a occurs more than once in the 2 words Again and Aarons.
i occurs more than once in the word inside.
p and n occurs more than once in the word happening.
So the array positions denoting the alphabet a, i, p and n contains the
values 2, 1, 1, 1 respectively.

YOUR ANSWER

https://www.hackerrank.com/tests/80bdrlfojm4/questions/7fjdiqar9gb

1/3

11/1/2015

Fresher [0Yr - 1Yr Exp] :: powered by HackerRank

1#include <math.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <assert.h>
6 #include <limits.h>
7 #include <stdbool.h>
8
9/*
10 * Complete the function below.
11 */
12int* countWords(char* P) {
13 int i,len,j=0,k,flag[26]={0},dup[26],count=0;
14 char ch[100];
15 len=strlen(P);
16 for(i=0;i<len;i++){
17 count=0;
18 while(P[i]!=' '){
19 ch[j]=tolower(P[i]);
20 j++;
21 count++;
22 }
23 i++;
24 j=0;
25 for(k=0;k<count;k++){
26 if('ch[k]'-97>=0 && 'ch[k]'-97<26){
27 flag['ch[k]'-97]++;
28 }
29
30 }
31 for(k=0;k<26;k++){
32 if(flag[k]>1){
33 dup[k]++;
34 }
35 flag[k]=0;
36 }
37 }
38 return dup;
39 }
40

https://www.hackerrank.com/tests/80bdrlfojm4/questions/7fjdiqar9gb

2/3

11/1/2015

Fresher [0Yr - 1Yr Exp] :: powered by HackerRank

41int main() {
42 FILE *f = fopen(getenv("OUTPUT_PATH"), "w");
43 int* res;
44 char* _P;
45 _P = (char *)malloc(10240 * sizeof(char));
46 scanf("\n%[^\n]",_P);
47 res = countWords(_P);
48 int res_i;
49 for(res_i=0; res_i < 26; res_i++) {
50
51 fprintf(f, "%d\n", res[res_i]);
52
53 }
54
55
56 fclose(f);
57 return 0;
58 }
59
Line: 9 Col: 1

Test against custom input

Download sample testcases

Run Code

Submit code & Continue

The input/output files have Unix line

endings. Do not use Notepad to edit them on windows.

About

Privacy policy

https://www.hackerrank.com/tests/80bdrlfojm4/questions/7fjdiqar9gb

Terms of service

3/3

You might also like