0

Character Array in C

Can any one tell me what is wrong in this program ? //******************************************************************** #include<stdio.h> int main() { char a[5]={' ',' ',' ',' ',' '},search; for(int i=0;i<=4;i++) { printf("\n Character %d:",i+1); scanf("%c",&a[i]); } printf("Enter character to be found"); scanf("%c",&search); for(int i=0;i<=4;i++) { if(a[i]==search) { printf("\n\n\t%c is found at %d",search,i+1); break; } if(i==4) { printf("\n\n\t%c is not found",search); } } return 0; }

3rd Aug 2019, 3:44 PM
yash nigam
yash nigam - avatar
2 Answers
+ 6
you need to enter the input on a separate line and your condition to check if its not found should rather be if(i>4) since you have an index 4 in the array put a space in the scanf like this scanf(" %c",); for both scanf
3rd Aug 2019, 6:40 PM
✳AsterisK✳
✳AsterisK✳ - avatar
0
yash nigam what is your expectation? I just gave input as below : a s d f g s and it worked
3rd Aug 2019, 4:41 PM
Ketan Lalcheta
Ketan Lalcheta - avatar