배우고픈 공돌이
code fighter - interview - str1 본문
You have been given a string s, which is supposed to be a sentence. However, someone forgot to put spaces between the different words, and for some reason they capitalized the first letter of every word. Return the sentence after making the following amendments:
- Put a single space between the words.
- Convert the uppercase letters to lowercase.
Example
- For
s = "CodefightsIsAwesome", the output should beamendTheSentence(s) = "codefights is awesome"; - For
s = "Hello", the output should beamendTheSentence(s) = "hello".
char * amendTheSentence(char * s) {
char buffer[strlen(s)*2];
int i=0;
char *ptr = s;
if(*s < 'a')
buffer[i++] = *s++ - 'A' + 'a';
else
buffer[i++] = *s++;
while(*s)
{
if(*s < 'a')
{
buffer[i++] = 32;
buffer[i++] = *s++ - 'A' + 'a';
}
else
buffer[i++] = *s++;
}
buffer[i]=0;
printf("%s",buffer);
s = ptr;
for(;i;i--)
*(s+i) = buffer[i];
*s = buffer[0];
return s;
}
Comments