Google Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Here is a solution in C
#include <stdio.h>
int camlConv(const char *in, char *out)
{
int iCount = 0,oCount = 0;
if (in == NULL)
{
out[oCount] = '\0' ;
}
while(in[iCount] != '\0')
{
if (in[iCount] <= 'Z' && in[iCount] >= 'A')
{
if (oCount != 0)
{
out[oCount++] = '_';
}
out[oCount] = in[iCount] + ('a' - 'A');
}
else
{
out[oCount] = in[iCount];
}
iCount++;
oCount++;
}
out[oCount] = '\0' ;
return oCount;
}
Here is a possible solution in Java and JUnit
import org.junit.Test;
//
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
//
public class CamelCaseToSnakeCaseTest {
//
@Test
public void testNullString() throws Exception {
assertThat(camelToSnake(null), is(nullValue()));
}
//
@Test
public void testBlankString() throws Exception {
assertThat(camelToSnake(" "), is(""));
}
//
@Test
public void testCamelString() throws Exception {
assertThat(camelToSnake("SomeGoodName"), is("some_good_name"));
}
//
private String camelToSnake(final String camelString) {
// if null return null
// that can be discussed
if (camelString == null) {
return camelString;
}
// if given a blank string return an empty one
String trimmedCamel = camelString.trim();
if (trimmedCamel.isEmpty()) {
return trimmedCamel;
}
// a buffer for the snake case result
StringBuilder snakeCase = new StringBuilder();
char snakeSeparator = '_';
// iterate over code points
int codePointCount = trimmedCamel.codePointCount(0, trimmedCamel.length());
for (int i = 0; i < codePointCount; i++) {
int codePoint = trimmedCamel.codePointAt(i);
// if the current code point is in upper case and not the first
if (Character.isUpperCase(codePoint) && i != 0) {
snakeCase.append(snakeSeparator);
}
// append lower case code point to the buffer
snakeCase.appendCodePoint(Character.toLowerCase(codePoint));
}
// convert the buffer to a string
return snakeCase.toString();
}
//
}
JavaScript, Ascii characters:
function work(str){
if(!str || !str.length)
return null;
var output='';
var upperACode= 'A'.charCodeAt(0);
var upperZCode = 'Z'.charCodeAt(0);
for(var i=0;i<str.length;i++){
var c = str.charCodeAt(i);
if(c>=upperACode && c<=upperZCode){
var prefix = i===0 ? '' : '_';
output += (prefix + str.charAt(i).toLowerCase());
}
else{
output += str.charAt(i);
}
}
return output;
}
/**
* time: O(N)
* space: O(N)
*/
private String toUnderscore( String str, char substitutionCh ){
final int offset = (int)'a' - (int)'A';
final int bigA = 'A';
final int bigZ = 'Z';
if( str == null ){
throw new IllegalArgumentException("NULL 'str' parameter passed");
}
char[] bufArr = new char[ (str.length() << 1) - 1 ];
int chCode;
int index = 0;
for( int i = 0; i < str.length(); i++, index++ ){
chCode = str.charAt(i);
if( chCode >= bigA && chCode <= bigZ ){
if( i > 0 ){
bufArr[index] = substitutionCh;
++index;
}
chCode += offset;
}
bufArr[index] = ( (char)chCode );
}
return new String(bufArr);
}
public class CamelCase {
static char capitalA='A';
static char capitalZ='Z';
public static void main(String args[]) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter the CamelCase String");
String camelCase = br.readLine();
char arr[] = camelCase.toCharArray();
String newString = "";
if(capitalA<= arr[0] && capitalZ >=arr[0]){
arr[0]=Character.toLowerCase(arr[0]);
}
newString = arr[0]+"";
for(int i=1;i< arr.length;i++){
if(capitalA<= arr[i] && capitalZ >=arr[i]){
arr[i]=Character.toLowerCase(arr[i]);
newString = newString+"_"+arr[i];
}
else {
newString=newString+arr[i];
}
}
System.out.println("new String is :"+newString);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here's what I got
public static void main(){
try{
String cam = CamelToSlug("AcceptThis");
//CamelToSlug(cam);
System.out.println(cam);
}
catch(IOException e){
e.printStackTrace();
}
}
public static String CamelToSlug(final String camCase) throws IOException{
if(camCase == null || camCase.isEmpty()){
throw new IOException(camCase + " empy value");
}
HashMap<String, Integer> slugSpot = new HashMap<String, Integer>();
//char[] cstr = camCase.toCharArray();
for(int i = 0; i < camCase.length(); i++){
char check = camCase.charAt(i);
if(check >= 'A' && check <= 'Z'){
slugSpot.put(String.valueOf(check),i);
}
}
char[] slugStr = new char[camCase.length() + slugSpot.size()-1];
camCase.toLowerCase();
System.out.println(slugStr.length);
System.out.println(slugSpot.get("A"));
for(int i = 0,j = 0; i < slugStr.length; i++){
if(i != 0 && slugSpot.containsValue(i)){
slugStr[i] = '_';
System.out.println("here");
j++;
}
else{
slugStr[i] = camCase.charAt(i-j);
if(i>=camCase.length()) j++;
}
}
String returnable = String.valueOf(slugStr);
return returnable.toLowerCase();
}
Here is C# solution.
public static string ToUnderscore(string input)
{
if (String.IsNullOrEmpty(input))
{
return null;
}
string result = null;
for (int i = 0; i < input.Length; i++)
{
if ('A' <= input[i] && input[i] <= 'Z')
{
if (i > 0)
result += '_';
result += char.ToLower(input[i]);
}
else
{
result += input[i];
}
}
return result;
}
Here is my solution using Perl and regular expressions:
use strict;
use Test::Simple tests => 7;
sub camel_to_underscore {
$_ = shift;
# Replace first capital by lowercase
# if followed my lowercase.
s/^([A-Z])([a-z])/lc($1).$2/e;
# Substitute camelCase to camel_case
s/([a-z])([A-Z])/$1.'_'.lc($2)/ge;
return $_;
}
# Tests using Test::Simple
ok(camel_to_underscore("HelloWorld") eq "hello_world");
ok(camel_to_underscore("helloWorld") eq "hello_world");
ok(camel_to_underscore("") eq "");
ok(camel_to_underscore("hello") eq "hello");
ok(camel_to_underscore("Hello") eq "hello");
ok(camel_to_underscore("HELLO") eq "HELLO");
ok(camel_to_underscore("h e l l o") eq "h e l l o");
Here's my code in python:
import sys
if len(sys.argv) == 1:
print "No input"
sys.exit()
inp = sys.argv[1]
output = ""
if inp[0].isupper():
output = inp[0].lower()
else:
output = inp[0]
for i in range(1, len(inp)):
if inp[i].isupper():
output += "_" + inp[i].lower()
else:
output += inp[i]
print output
c++ code below:
#include <iostream>
#include <string>
using namespace std;
string CCtous(string input)
{
int inlength = input.length();
string output = "";
for (int ii=0; ii<inlength; ii++)
{
char iichar = input[ii];
if ((int)iichar >= 'A' && (int)iichar <= 'Z')
{
int asciival = (int)iichar;
char conv = (char)( 'a' + asciival - 'A');
if (ii!=0)
{
output = output + "_";
}
output = output + conv;
}
else
{
output = output + iichar;
}
}
return output;
}
int main()
{
string test = "NaCl";
cout<<CCtous(test)<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int count = 0;
char *p = "helloWorld!!!";
for (int i = 1; i < strlen(p); i++) {
if(p[i] > 64 && p[i] < 91){
count++;
}
}
int newLength = strlen(p) + count;
char *result = new char[newLength];
for (int i = 0, j = 0; i < strlen(p); i++,j++) {
if(p[i] > 64 && p[i] < 91){
if(i == 0){
result[0] = p[0] + 32;
}
else {
result[j++] = '_';
result[j] = p[i] + 32;
}
}
else {
result[j] = p[i];
}
}
for (int i = 0; i < strlen(result); i++) {
cout << result[i];
}
}
private static String toUnderscoreCase(String s) {
if (s == null || "".equals(s))
return s;
char[] c = s.toCharArray();
char[] r = new char[c.length * 2];
int j = 0;
for (int i = 0; i < c.length; i++) {
if (i > 0 && isUpperCase(c[i])) {
r[j++] = '_';
}
r[j++] = toLowerCase(c[i]);
}
return new String(r, 0, j + 1);
}
private static char toLowerCase(char c) {
if (isUpperCase(c)) {
return (char) (c+32);
}
return c;
}
private static boolean isUpperCase(char c) {
return c > 64 && c < 91;
}
def convertTitle(title):
"""Convert Camilcase to underscore-lowercase.
>>> convertTitle('')
''
>>> convertTitle('Hello')
'Hello'
>>> convertTitle('upperCase')
'upper_case'
"""
newTitle = ''
if title == '':
return newTitle
else:
for ind in range(len(title)):
if ind > 0:
if title[ind].upper() == title[ind]:
newTitle = newTitle + '_' + title[ind].lower()
else:
newTitle = newTitle + title[ind]
else:
newTitle = title[ind]
return newTitle
if __name__ == '__main__':
import doctest
doctest.testmod()
print(convertTitle('upperCase'))
- BerMuc June 11, 2014