简体   繁体   English

指针句子大小写(C)

[英]pointer sentence case (C)

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 */`

Once again. 再来一次。 After starring a bit more at the code I saw that 在看了一些代码之后,我看到了

  • you manipulated the pointer text_ptr++ 您操纵了指针text_ptr++
  • incremented i only in the else part of the loop 仅在循环的else部分增加i
  • printed text_ptr++ and text_ptr+i 打印的text_ptr++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. 请注意,此版本确实需要再次toupper Otherwise correct I will be lowered. 否则正确的I将被降下。 Your fourth if works fine as well (I oversaw, that you don't reset the up flag for spaces). 您的第四个if也很好(我监督,您不重置空格的up标志)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM