0

I am trying to take t (test cases) strings as inputs in C. Every string ends with a # and it can have spaces.

I am using getchar and some while loops but I am not able to achieve what I want.

int t;
scanf("%d",&t);

while(t>0){


        char expression[100];
        char c;

        int i=0;
        while(1){
            c = getchar();
            if(c == '#'){
                break;
            }
            else{
                expression[i]=c;
                i++;
            }

        }

        t--;


        printf("%d\n",i);
        for(int j=0;j<i;j++){
            printf("%c",expression[j]);
        }
      }

What I am trying to achieve is if # is encountered I want to ignore whatever is after It.

My input:

1
test#wtf

Output:

5

test

So in case of 1 test case it is working fine. But when I give try this.

2
test#wtf
test#

The corresponding outs are:

5

test

and

8
wtf
test

As you can see the second output should be test and length should be 5 but it is not so.

  • The code you posted is not a [mcve]. What is the value of variable `t` **before** entering the [outer] `while` loop? – Abra Sep 05 '19 at 09:47
  • 1
    I'm sure you can figure it out if you try. Your problem is you stop reading characters at # and resume in the next iteration. Instead of just stopping at # you should instead read the rest until you find `endl` and then stop. Alternatively you can read input line by line instead of one character at a time, store it into a temporary string and then work with that string. I believe `scanf` is the proper function for that. – IcedLance Sep 05 '19 at 09:48
  • @Abra t is just a positive number I'll edit the code for better explanation – Im retarded but Sep 05 '19 at 09:48
  • @IcedLance the strings can have spaces, how can I use scanf? – Im retarded but Sep 05 '19 at 09:53
  • @Imretardedbut my bad. gets() is what you want. Edit: actually can use scanf too. Can see how [here](https://www.google.com/amp/s/www.geeksforgeeks.org/difference-between-scanf-and-gets-in-c/amp/) – IcedLance Sep 05 '19 at 09:55
  • 1
    @IcedLance there is no [`gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) any more. Use `fgets` instead. – Jabberwocky Sep 05 '19 at 11:46

2 Answers2

3

In order to ignore text after #, you'll have to consume those characters using getchar() and ignore them till you encounter a new line character '\n'.

So, after you exit the inner while loop, you need to add the following code

while(1) {
    c = getchar();
    if(c == '\n'){
        break;
    }
}
Suraj
  • 602
  • 3
  • 16
1

You can do it this way, you process all characters, once you get a '#' you keep reading characters without adding them to the array and you get out of the outer loop once you get a new line character '\n'.

while (--t > 0)
    {
        int i,s,c;
        char exp[100];

        s = 0;
        while (s < 100 && (c = getchar()) != '#')
            exp[s++] = c;
        while ((c = getchar()) != '\n')
            ;
        printf("%d\n", s);
        i = -1;
        while (++i < s)
            printf("%c", exp[i]);
        printf("\n");
    }
XBlueCode
  • 785
  • 3
  • 18