Magical String II
Magical Girl Emily uses "magical strings" to cast spells. For her, a string X is magical if and only there are no consecutive '(' or ')' characters . The string is invalid if it contains any character other than '(' and ')'.
Given a string, write a program to find whether it is magical or not.
 
Input and Output Format:
Input consists of a string. Assume that the maximum length of the string is 100.
Output 'invalid' if the string is invalid. Output 'yes' if the string is magical or 'no' if the string is not magical.
 
Sample Input 1:
()()(
Sample Output 1:
yes

Sample Input 2:
(()()
Sample Output 2:
no

Sample Input 3:
(((&))
Sample Output 3:
invalid
  #include
02#include
03int main()
04{
05  char a[100];
06  int i=0,j=0,f=0;
07  gets(a);
08  while(a[i]!='\0')
09  {
10    if(a[i]!='(' && a[i]!=')')
11    {
12     f=1;
13     break;
14    }
15    i++;
16  }
17  i=0;
18  if(f==0)
19  {
20    while(a[i]!='\0')
21    {
22      if(a[i]==a[i+1])
23      {
24        j=1;
25      break;
26      }
27      i++;
28    }
29  }
30  if(j==1 &&f==0)
31    printf("no");
32  else if(j==0 && f==0)
33    printf("yes");
34    else
35      printf("invalid");
36  return 0;
37}

No comments: