0

I have problem to output text in sentence case characters. When I prompt: heLLo! heLLo. i am OKAY.

I'm expecting output: Hello! Hello. I am okay.

But my sample run output is: Hello! hello. i am okay.

My code cannot output uppercase after '!'/'.'/'?'/'_' Anybody can advise what mistake that I made? Thanks in advance.

-Ellie

Sample code:

printf ("\n\nThe line of the text in sentence case is:\n");

i = 0;
text_ptr = text;
up = 1;                 /* up = 1 means upper case is yes */
while ( *(text_ptr+i) != '\0')  /* could have been: while(text[i]) */
{
    if(!up)
        if( *(text_ptr+i-1)==' ' && toupper(*(text_ptr+i)=='I' && (*(text_ptr+i+1)==' ' ||
                *(text_ptr+i+1)=='.' || *(text_ptr+i+1)=='!' || *(text_ptr+i+1))=='?') )
        up = 1;     /* capitalize i if all alone */

    if(up)
        if (*(text_ptr+i)!=' ' || *(text_ptr+i+1)=='.' || *(text_ptr+i+1)=='!' || *(text_ptr+i+1)=='?')
        {
            putchar(toupper(*(text_ptr++)));
            up = 0;
        } /* end if */
        else
            putchar(tolower(*(text_ptr++)));
    else
    {
        putchar(tolower(*(text_ptr+i)));
        if (*(text_ptr)=='?' || *(text_ptr)=='.' || *(text_ptr)=='!')
            up = 1;
        i++;
    } /* end else */
}/* end while */`
Anto Jurković
  • 11,188
  • 2
  • 29
  • 42
Elski
  • 29
  • 9
  • 2
    Did you try to debug this yourself? If not you might like to read this: http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – alk Mar 14 '15 at 09:42
  • `i` used incorrect. It does not seem necessary. – BLUEPIXY Mar 14 '15 at 09:58
  • Notes: 1) use some `{braces}` for multi-line blocks. 2) use indexing instead of the pointer notation. 3) consider a finite state machine - approach. – wildplasser Mar 14 '15 at 13:28

1 Answers1

0

Once again. After starring a bit more at the code I saw that

  • you manipulated the pointer text_ptr++
  • incremented i only in the else part of the loop
  • printed text_ptr++ and text_ptr+i

... what is hard to understand So I completely revised my version:

int i  = 0;
int up = 1; /* up = 1 means next char should be upper case*/
char* text_ptr = text;

while (*(text_ptr+i) != '\0') { /* could have been: while(text[i]) */
    if(!up)
      if(*(text_ptr+i-1)==' ' && toupper(*(text_ptr+i))=='I' &&  
        (*(text_ptr+i+1)==' ' || *(text_ptr+i+1)=='.' ||  // fix bracket here
         *(text_ptr+i+1)=='!' || *(text_ptr+i+1)=='?')) { // "i" foll. by one of those       
           up = 1;    /* capitalize i if all alone */
      }

    if(up)
      if (*(text_ptr+i)!=' ' && *(text_ptr+i)!='.' && // fix here
          *(text_ptr+i)!='!' && *(text_ptr+i)!='?') { // anything else than these
            putchar(toupper(*(text_ptr+i))); // toupper and reset up
            up = 0;
        } /* end if */
        else
            putchar(tolower(*(text_ptr+i))); // just print
    else
    {
        putchar(tolower(*(text_ptr+i)));
        if (*(text_ptr+i)=='?' || *(text_ptr+i)=='.' || *(text_ptr+i)=='!')
            up = 1;
    } /* end else */
    i++;
}/* end while */

Note that this version does require the toupper again. Otherwise correct I will be lowered. Your fourth if works fine as well (I oversaw, that you don't reset the up flag for spaces).

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • thanks for your answer. I just edit my code as per your suggestion. The output is still not as I expected. Become: Hello! hello. E am okay. I use : toupper(*(text_ptr+i)=='I', because I want to output i to become I. I really confuse now.. – Elski Mar 14 '15 at 10:29
  • One small error - move one bracket as per edit (again `toupper`) ... http://codepad.org/1eZsrKaZ – Trinimon Mar 14 '15 at 11:22
  • @ellievanjv: did you check the CodePad example? – Trinimon Mar 14 '15 at 13:31
  • Yes, I did. I ran it with my whole codes, and it works. I understand where is my mistakes now. Thank you for your help! – Elski Mar 15 '15 at 05:16