2012 Beihang Machine Test:
T3: Statistic keyword Enter a line of standard C language code (the number of characters is less than 300), statistics the keyword in the string location of the if,while,for, according to the order in which the keyword appears in turn output. Note that the double quotes do not require statistics.
Input: One line of standard C language code, the number of characters is less than 300
Output: keyword if,while,for corresponding position, output according to the order in which keywords appear. The output format is: keyword, followed by a colon, and then where it appears. The output is scanned to the keyword, and each output occupies one row.
Sample input:
#include <stdio.h> int main () {int i = 0; if (i = = 0) printf ("YES"); return 0;}
#include <stdio.h> int main () {int ifwhile = 0; int Forif = 1;char If_for_while = ' a '; char *str = ' while '; (IFW Hile = = 0) {ifwhile = 1;forif = 0;} if (Forif = = 0) {if_for_while = ' B ';} if (Ifwhile = 1) {if_for_while = ' C ';} return 0;}
Sample output:
if:43
while:88
if:133
if:170
This problem feels like the person who remembers the problem has mistaken the answer the example 2 output should be while:108 if:153 if:190
Analysis: Encountered "on the use of loops to skip the double quotes within the characters
symbols that may appear before and after the if:
If
}if
}if
; if
If both before and after can be followed by spaces
the same characters may appear before or after a while
The source code is as follows:
#include <stdio.h>
#include <string.h>
char s[302];
int main () {
int i,w;
int Len;
Freopen ("3.txt", "R", stdin);
i=0;
w=0;
while (gets (s)) {
Len=strlen (s);
for (i=0;i<len;i++) {
if (s[i]== ' ") while
(s[i]!= '") i++;
if (s[i]== ' I ') {
if (s[i+1]== ' F ' &&
(s[i+2]== ') (' | | s[i+2]== ') &&
(s[i-1]== ' | | s[i-1]== '} ' | | s[i-1]== ' {' | | s[i-1]== '; ')
printf ("if:%d\n", i+1);
}
else if (s[i]== ' W ') {
if (s[i+1]== ' h ' &&s[i+2]== ' I ' &&s[i+3]== ' l ' &&s[i+4]== ' E ' & &
(' s[i+5]== ' | | s[i+5]== ')
&& (s[i-1]== '; s[i-1]== ' ' | | s[i-1]== '} ' | | s[i-1]== ' {')
printf ("while:%d\n", i+1);
}}
return 0;
}