0% found this document useful (0 votes)
3 views4 pages

Day 2 Answer Key

The document contains implementations of a 'camelcase' function in Python, Java, and C that count the number of words in a camel case formatted string. Each implementation initializes a count, iterates through the string to identify uppercase letters, and returns the total count of words. The Python version reads input from the environment, while the Java and C versions handle input through standard input and file operations.

Uploaded by

islamsmytheen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Day 2 Answer Key

The document contains implementations of a 'camelcase' function in Python, Java, and C that count the number of words in a camel case formatted string. Each implementation initializes a count, iterates through the string to identify uppercase letters, and returns the total count of words. The Python version reads input from the environment, while the Java and C versions handle input through standard input and file operations.

Uploaded by

islamsmytheen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

USING PYTHON​

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'camelcase' function below.
#
# The function is expected to return an INTEGER.
# The function accepts STRING s as parameter.
#

def camelcase(s):
count=0 #initial value of count
for i in s:
if i.isupper():
count=count+1
count=count+1 #adding count of the first word which starts with small letter
return count

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

s = input()

result = camelcase(s)

fptr.write(str(result) + '\n')

fptr.close()

USING JAVA 8
class Result {

/*
* Complete the 'camelcase' function below.
*
* The function is expected to return an INTEGER.
* The function accepts STRING s as parameter.
*/

public static int camelcase(String s) {


// Write your code here
int count =1;
int n = s.length();
for(int i=0;i<n;i++){
if(Character.isUpperCase(s.charAt(i)))
count ++;
}
return count;
}

USING C

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();

/*
* Complete the 'camelcase' function below.
*
* The function is expected to return an INTEGER.
* The function accepts STRING s as parameter.
*/

int camelcase(char* s) {
int count = 1; // First word is lowercase

for (int i = 0; s[i] != '\0'; i++) {


if (isupper(s[i])) {
count++;
}
}

return count;
}

int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

char* s = readline();

int result = camelcase(s);

fprintf(fptr, "%d\n", result);

fclose(fptr);

return 0;
}

char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;

char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) {
break;
}

data_length += strlen(cursor);

if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {


break;
}

alloc_length <<= 1;
data = realloc(data, alloc_length);

if (!data) {
data = '\0';

break;
}
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';

data = realloc(data, data_length);

if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);

if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}

return data;
}

You might also like