简体   繁体   English

使用IN / OUT标志每行显示一个单词一个字符串

[英]Displaying a string one word per line using IN/OUT flag

I have written a program that first stores an arbitrary number of lines of text from the user. 我编写了一个程序,该程序首先存储来自用户的任意数量的文本行。 After that, it checks when a new word has come and if it does, then it prints it in a new line. 之后,它将检查何时有新单词出现,如果有,则将其打印在新行中。 Below is my code: 下面是我的代码:

 #include<stdio.h>
 #define IN 1   //inside a word
 #define OUT 0  //outside a word

int main()
{
  char s[100];   //storing the string entered by user
  int c;         //getchar reading variable
  int i=0;       //iterating variable

  while((c=getchar())!=EOF)
  {
    s[i++]=c;
  }
  s[i]='\0';   //end of string


  i=0;
  int p=0;    //stores the start of the word
  int current=OUT; //flag to indicate if program is inside or outside a word

  while(s[i]!='\0')  
  {
    if(current==OUT)  //when program is outside a word
    {
        if(s[i]!=' ' || s[i]!='\n' || s[i]!='\t')  //word found
        {
            p=i;  //store starting position of word 
            current=IN;
        }
    }
    else if(current==IN) //program is inside a word
    {
        if(s[i]==' ' || s[i]=='\n' || s[i]=='\t') //end of word found
        {
            current=OUT; //flag now outside the word
            for(int j=p;j<i;j++) //print from starting position of word
            {
                printf("%c",s[j]);
            }

            printf("\n");  //go next line

        }

    }
    ++i;  //incremnent the iterator variable
 }

return 0;
}

My program works well if I just enter the string in a proper manner, ie without any extra spaces or new lines. 如果我以适当的方式(即没有任何多余的空格或换行)输入字符串,则我的程序将运行良好。 But if I enter a line as follows ( notice the extra spaces and new lines ): 但是,如果我按如下方式输入一行(请注意多余的空格和新行 ):

*I am a boy *我是一个男孩

I went to Japan */ 我去了日本* /

Then it prints those extra newlines and spaces along with word too, which according to me should not happen because of the IN and OUT flags. 然后,它还会打印这些多余的换行符和空格以及单词,根据我的看法,由于IN和OUT标志,这应该不会发生。 The output is like this: enter image description here I request you to please help me out. 输出是这样的:请在此处输入图片描述 ,请您帮助我。

I know I can do this easily with the putchar() method checking one character at one time, but I am just curious as to what I am doing wrong in this implementation. 我知道我可以轻松地通过一次检查一个字符的putchar()方法来做到这一点,但是我只是好奇我在此实现中做错了什么。

First bug that jumps out at me: 跳到我身上的第一个错误:

if(s[i]!=' ' || s[i]!='\n' || s[i]!='\t')

will always return true. 将始终返回true。 You want && , or else use a !() around the whole condition that you use the other place, for symmetry. 您需要&& ,或者在使用其他位置的整个条件周围使用!()进行对称。

Or better yet, factor that out into a function, or use isspace from <ctype.h> . 或者更好的是,将其分解为函数,或使用<ctype.h> isspace

Your filtering condition for determining if a character is white space is not correct. 您用于确定字符是否为空格的过滤条件不正确。 The || || operator means OR. 运算符表示“或”。 Using chained OR will allow the expression to evaluate to true every time. 使用链式OR可使表达式每次都评估为true。 You need the AND operator && . 您需要AND运算符&& The and operator fails as soon as one operand evaluates to false, or in the case of C, 0 . 一旦一个操作数的计算结果为false,则and运算符将失败,或者对于C而言,该运算符为0

Besides that, there are better ways to check for white space. 除此之外,还有更好的方法来检查空白。 One idea is using the isspace function from <ctype.h> , which accepts a character as an int , which can also be an unsigned char , and determines if that character is any of ' ', '\\t', '\\v', '\\n' or '\\r' . 一种想法是使用<ctype.h>isspace函数,该函数接受一个字符作为一个int ,也可以是一个unsigned char ,并确定该字符是否为' ', '\\t', '\\v', '\\n' or '\\r' You can also do character checking via switch statements 您还可以通过switch语句进行字符检查

switch(ch) {
    case ' ':
       // do something
        break;
    case '\n':
      //do something
        break;
}

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

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