Created
August 27, 2023 21:52
-
-
Save Tomi-3-0/d702d81d8f4141f9972b4ef04c44052d to your computer and use it in GitHub Desktop.
Solution to Hackerrank's camel case 4 in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// # Enter your code here. Read input from STDIN. Print output to STDOUT | |
// # Each line of the input file will begin with an operation (S or C) followed by a semi-colon followed by M, C, or V | |
// # The operation will either be S (split) or C (combine) | |
// # M indicates method, C indicates class, and V indicates variable | |
// # In the case of a split operation, the words will be a camel case | |
// # In the case of a combine operation, the words will be a space-delimited list of words starting with lowercase letters that you need to combine into the appropriate camel case String | |
// # Methods should end with an empty set of par | |
#include <stdio.h> | |
#include <string.h> | |
#include <math.h> | |
#include <stdlib.h> | |
// trim from start (in place) | |
static inline void ltrim(char *s) { | |
while (isspace(*s)) { | |
s++; | |
} | |
} | |
// trim from end (in place) | |
static inline void rtrim(char *s) { | |
char *back = s + strlen(s); | |
while (isspace(*--back)); | |
*(back + 1) = '\0'; | |
} | |
// trim from both ends (in place) | |
static inline void trim(char *s){ | |
ltrim(s); | |
rtrim(s); | |
} | |
void addSpaceBeforeCapital(char *str) { | |
int length = strlen(str); | |
char result[2 * length]; // To accommodate possible additional spaces | |
int resultIndex = 0; | |
for (int i = 0; i < length; i++) { | |
result[resultIndex++] = str[i]; | |
if (i > 0 && isupper(str[i + 1])) { | |
result[resultIndex++] = ' '; | |
} | |
} | |
result[resultIndex] = '\0'; // Null-terminate the result | |
strcpy(str, result); // Copy the result back to the original string | |
} | |
int main() { | |
char line[1000]; | |
while (fgets(line, sizeof(line), stdin)) { | |
//split line | |
char action = line[0]; | |
char mvcn = line[2]; | |
char s[1000]; | |
strcpy(s, line + 4); | |
// start | |
if (action == 'S') { | |
if (mvcn == 'M') { | |
s[strlen(s) - 3] = '\0'; | |
} else if (mvcn == 'C') { | |
s[0] = tolower(s[0]); | |
} | |
char *s1 = " "; | |
addSpaceBeforeCapital(s); | |
for (int i = 0; i < strlen(s); i++) { | |
if (s[i] >= 'A' && s[i] <= 'Z'){ | |
s[i] = tolower(s[i]); | |
} | |
} | |
} else if (action == 'C') { | |
char *space; | |
while ((space = strchr(s, ' '))) { | |
*space = toupper(*(space + 1)); | |
memmove(space + 1, space + 2, strlen(space + 1)); | |
} | |
if (mvcn == 'M') { | |
strcat(s, "()"); | |
} else if (mvcn == 'C') { | |
s[0] = toupper(s[0]); | |
} | |
} | |
trim(s); | |
printf("%s\n", s); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment