Along with other suggestions mentioned in the comments like -
- Changing
main signature to return int
- Update
a[i]=='T'; and a[i]=='H'; to remove double equals warning inside if blocks (simply replacing == by = would be logically incorrect, see @CiaPan's answer below)
you also have to update scanf statement
- scanf("%c",&a[i]); to scanf(" %c",&a[i]); (note the extra space at the beginning before %c)
This should solve the segmentation fault issue.
See this and this for details -
%d automatically eats whitespaces and special characters since whitespaces and special characters are characters, not numbers! However, %c has to interpret whitespaces and special characters as inputs, because %c reads characters.
Explanation -
On adding few printf statements in the program like below to check the input received, we can see what happens -
int N=0,K,count=0,i; // N initialized to 0, but not K
printf("Before scan N=%d, K=%d\n", N, K);
i=scanf("%d%d",&N,&K);
char a[200];
printf("After scan-\n N=%d, K=%d, Scanf return value=%d\n", N, K, i);
for(i=0;i<N;i++){
scanf("%c",&a[i]);
}
for(i=0;i<N;i++){
printf("%d=>'%c', ",i, a[i]);
}
printf("\n");
with input -
3
5 3
H T T H T
7 4
H H T T T H H
6 1
T H T H T T
The output of whole program looks something like this -
Before scan N=0, K=1362879360
After scan-
N=5, K=3, Scanf return value=2 <------ 2 VALUES SUCCESSFULLY READ
0=>'
', 1=>'H', 2=>' ', 3=>'T', 4=>' ',
Program output - 1
Before scan N=0, K=-1
After scan-
N=0, K=-1, Scanf return value=0 <------ 0 VALUES SUCCESSFULLY READ (problem)
exited, segmentation fault
As mentioned by @CiaPan in the comment, %c consumes each character, even spaces and newlines in the array, but leaving part of the input line H T T H T in the input buffer. Since N and K are positive and N greater than K in the first loop, the second while loop terminates.
During second iteration of main loop (test case), N is initialized to 0 and K has value -1 from the first loop iteration. Since leftover data from input buffer cannot be assigned correctly to N and K, they continue with this value.
Now for second while loop (test case), K starts negative, so the condition while(K--) stays correct for unexpected long time (until K is zero after wrapping at integer boundary), and N keeps decreasing as well. Eventually for some large negative value of N, the program tries to access inaccessible memory (with a[N-1]), leading to segmentation fault.
This issue can be avoided by ensuring that N and K are properly initialized and have intended values throughout the program. The space before %c in scanf would consume the extra whitespace/newline from the input buffer properly to supply correct values to array and thereby leading to N,K variables being assigned from input as expected. Input issues like this can be caught by checking the return values of scanf function. Additional checks/assertions like K>0 can be helpful for debugging purposes.