i fixed it to
if (*input==')'){
and it ran and said that its valid
but when I changed the input to "(1,2) (34,5) (6 7,8)"
It also says that its valid.
??
whats incomplete?
Code:
#include <stdio.h>
#include <ctype.h>
int is_valid(const char *input);
const char *match_pair(const char *input);
int main()
{
char input[] = "(1,2) (34,5) (6 7,8)";
if (is_valid(input))
{
printf("%s is valid.\n", input);
}
else
{
printf("%s is invalid.\n", input);
}
return 0;
}
/* Match one or more consecutive digits and return a pointer to the first char
* that is not a digit.
*/
const char *match_number(const char *input)
{
while((*input<='9')&&(*input>='0')){
input++; //increasing the address by1
}
return input;
}
/* Match "(<number>,<number>)" pattern.
* Return a pointer to the first char after the matched pattern, or
* NULL if there is no match.
*/
const char *match_pair(const char *input)
{
if (*input != '(')
{
return NULL;
}
input = match_number(input + 1); //it wiil get char '2'
if (*input == ',') { //'2' differs ','
input = match_number(input + 1);
if (*input==')'){
return input;
}
else
return NULL;
}
else
return NULL;
}
/* Match "(<number>,<number>) (<number>,<number>) (<number>,<number>)" pattern.
* Return 1 if there is a match and 0 otherwise.
*/
int is_valid(const char *input)
{
input = match_pair(input);
if (input == NULL)
{
return 0;
}
else{
return 1;
}
}