The 12 machine test of Beihang--the realization of C language of statistic key word

Source: Internet
Author: User

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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.