CodingSpace

[HackerRank/Algorithms] Strings - CamelCase 본문

HackerRank/Algorithm

[HackerRank/Algorithms] Strings - CamelCase

개발자_조이킴 2022. 9. 6. 00:23

Problem. Strings - CamelCase


Link.

https://www.hackerrank.com/challenges/camelcase/problem?isFullScreen=true 

 

CamelCase | HackerRank

 

www.hackerrank.com


Description.

There is a sequence of words in CamelCase as a string of letters, s, having the following properties:

  • It is a concatenation of one or more words consisting of English letters.
  • All letters in the first word are lowercase.
  • For each of the subsequent words, the first letter is uppercase and rest of the letters are lowercase.

Given s, determine the number of words in s.

 


Key Point. 


My Answer. 

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

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

function camelcase(s) {
    let cnt = 0;
    
    for(let i = 0; i < s.length; i++) {
    // 만약 i번째 문자가 대문자이면 cnt + 1
        if(s[i] === s[i].toUpperCase())
            cnt++;
    }
    
    return cnt + 1;
}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const s = readLine();

    const result = camelcase(s);

    ws.write(result + '\n');

    ws.end();
}

References. 

 

Comments