배우고픈 공돌이

code fighter - interview - str1 본문

C, C++ /C

code fighter - interview - str1

내 마음 아홉수 2017. 12. 4. 18:08

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 be
    amendTheSentence(s) = "codefights is awesome";
  • For s = "Hello", the output should be
    amendTheSentence(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; 
}


'C, C++ > C' 카테고리의 다른 글

범용 큐  (0) 2018.01.13
범용 스택  (0) 2018.01.13
등가 포인터  (0) 2017.08.11
volatile  (0) 2017.08.07
전처리  (0) 2017.07.29
Comments