In: Computer Science
In C programming language how do you find if all the character on a single line in a 2 dimensional array are the same?
The program should read line by line to check if all the characters on a line are the same. For this program we want to see if the * character ever shows up on a line where it is the only character on the line. as seen below.
so lets say we have a array with the following data:
Note(this is a 5x7 two dimensional array)
D C D D * D C
D C C C D * *
* * * * * * *
D C C D * D *
* * * * * * *
So i want to return the amount of lines that have the * in every position in that row. So in this array 2 lines have all the same characters which is a * so it should print "The amount of lines where the * character is the only character that appears is 2"
Ans
code:-
#include<stdio.h>
int main(){
int ans=0,j=0;
int r=5,c=7;//size of matrix
char a[5][7]={{'D','C','D','D','*','D','C'},{'D','C','C','C','D','*','*'},{'*','*','*','*','*','*','*'},{'D','C','C','D','*','D','*'},{'*','*','*','*','*','*','*'}};
//for each row
for(int i=0;i<r;i++){j=0;
for( j=0;j<c;j++){//for each col
if(a[i][j]!='*'){//if no *,next row
break;
}
}
if(j==c){//if all *
ans++;
}
}//print the result
printf("The amount of line where the * character is the only character is %d",ans);
}
If any doubt ask in the comments.