I wrote a program which recognizes the following pattern
(num,num) (num,num) (num,num)
num is an integer number.
I took a copy of the input array to temp array.
then i changed temp like this:
first i transformed every space char ' ' into '' (no char)
then i transformed ever number before ',' to 4 and after ',' to 5 .
so my legal input should look like (4,5) (4,5) (4,5)
then if the char in the loop is ( it should expect 4,5)
if the char is 4 it should expect ,5) after it and ) before it
There are five terms to be completed each cycle
if the counter%5 =0 then the input is legal.
i wrote this code but as i showed in the code by comments it has errors
how to fix them?
Code:
#include <stdio.h>
int check_if_legal(char mines[],int length);
int main() {
int index;
char str[40];
int i;
int length;
printf("enter length\n");
scanf("%d",&length);
printf("enter string\n");
for(index=0;index<length;index++){
scanf("%c",&str[index]); //13
}
i=getchar();
printf("%d",check_if_legal(str,length));
return 0;
}
int check_if_legal(char mines[],int length)
{
int state=-1;
int counter=0;
int flag_o=0;
int flag_failure=0;
int flag_z=0;
int index2;
int index;
char temp[40];
for(index=0;index<length;index++){
temp[index]=mines[index]; //13
}
for (index=0;index<length;index++){
if (temp[index]==' '){
temp[index]=''; ////|39|empty character constant|
if ((temp[index]<='9')&&(temp[index]=>'0')){ //|41|error: syntax error before '>' token|
temp[index]='4';
if (temp[index-1]=','){
temp[index]='5';
}
index2=index+1;
while((temp[index2]<='9')&&(temp[index2]=>'0')){//|47|error: syntax error before '>' token|
temp[index2]=''; //|48|empty character constant|
index2++;
}//end while
}//end if
}//end if
}//end for
for (index=0;index<length;index++){ // (4,5)
if(temp[index]=='('){
if((temp[index+1]=='4')&&(temp[index+2]==',')&&(temp[index+3]=='5')&&(temp[index+4]==')')){
counter++;
}
else
{
state=1;
}
}
if(temp[index]=='4'){
if((temp[index-1]=='(')&&(temp[index+1]==',')&&(temp[index+2]=='5')&&(temp[index+3]==')')){
counter++;
}
else
{
state=0;
}
}
if((temp[index]==','){
if((temp[index-1]=='4')&&(temp[index-2]=='(')&&(temp[index+1]=='5')&&(temp[index+2]==')')){
counter++;
}
else
{
state=0;
}
}
if((temp[index]=='5')){
if((temp[index-3]=='(')&&(temp[index-1]==',')&&(temp[index-2]=='4')&&(temp[index+1]==')')){
counter++;
}
else
{
state=0;
}
}
if((temp[index]==')'){
if((temp[index-1]=='5')&&(temp[index-2]==',')&&(temp[index-3]=='4')&&(temp[index-4]==')')){
counter++;
}
else
{
state=0;
}
}
}//end for
if ((counter>0)&&(counter%5==0){
state=1;
}
return state;
}//end func