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.