This is my machine exam for the C language competition.

Source: Internet
Author: User

Question 1:

# Include <stdio. h> # include <string. h> # include <stdlib. h> # define N 30 /********************************* * ************** Function Name: fun * Creation Time: 2010.12.5 * Author: huangliangming * Description: sorts a string, with the letters at the top and numbers at the bottom, it does not change the character sequence between original letters and numbers. * Parameter: char * s, int * m * return value: chletter (first element address of the array chletter []) * local variable: Char chletter [N]; * char chnumber [N]; * int I, J, K; **************************************** * ******/char * Fun (char * s, int * m) // The Parameter m is generated after debugging. {char chletter [N]; // It is used to store the letter char chnumber [N]; // It is used to store the number int I, j, k; I = 0; // initialize J = 0; // J is used to record the number of letters K = 0; // K is used to record the number of numbers for (I = 0; I <n; I ++) {If (s [I]> = 'A' & S [I] <= 'Z' // Save the letter to chletter [] | s [I]> =' a' & S [I] <= 'Z ') {chletter [J] = s [I]; j ++;} If (s [I]> = '0' & S [I] <= '9 ') // Save the number to chnumber [] {chnumber [k] = s [I]; k ++ ;}} chletter [J] = '\ 0 '; chnumber [k] = '\ 0'; * m = J + k; // returns the number of characters in the last Input and Output. strcat (chletter, chnumber); Return chletter ;} // main function void main () {char s [N]; int I; int m; char * P; P = NULL; printf ("enter a string (less than 30 characters): \ n "); scanf (" % s ", S); P = fun (S, & M ); // At the beginning, this m was not defined to limit the length of the array indicated by the pointer P. The following two characters are garbled for (I = 0; I <m; I ++) // copy the returned value to the array to be output {s [I] = P [I];} printf ("Result:"); for (I = 0; I <m; I ++) // output result {printf ("% C", s [I]);} printf ("\ n ");}

 

Question 2:

# Include <stdio. h> # include <string. h> # include <stdlib. h> # define N 30 // s refers to the maximum length of the string # define T 2 // The length of the string indicated by T1 and T2 /************ * *********************************** Function Name: fun * Creation Time: 2010.12.5 * Author: huangliangming * description: replace the last occurrence of the substring in the string s that is the same as that in the string T1 with the string * parameter: char * s, char * T1, char * t2, int * m * return value: w (address of the first element of the array W []) * local variable: Char W [N]; * char temp [T]; * char t1temp [T]; * int I, J, K, L; **************************************** * ******/char * Fun (char * s, char * T1, char * t2, int * m) // The role of m is the same as that of the first question. The last two characters without m will be garbled (for other methods) {char W [N]; // It is used to store the processed string char temp [T]; // It is used to store the substring char t1temp [T] intercepted from the string indicated by S; // used to store the int I, J, K, L strings specified by T1; // store the for (I = 0; I <t; I ++) string specified by T1 to t1temp) {t1temp [I] = T1 [I];} t1temp [T] = '\ 0'; // evaluate the value referred to by m for (I = 0; I <N; I ++) {If (s [I] = '\ 0') {* m = I ;}} // search for the subscript for (I = 0; I <n; I ++) {L = 0; For (j = I; j <(I + T); j ++, l ++) // truncate the child string with a length of T and store it in temp [] {temp [l] = s [J];} temp [T] = '\ 0 '; if (strcmp (t1temp, temp) = 0) {k = I; // subscript of the last character in the same K record} for (I = 0; I <n; I ++) // assign a value to W [] {J = 0; if (I >=k & I <(K + T )) // start changing the value at the K point {W [I] = t2 [J]; // changing the value J ++ ;} else {W [I] = s [I] ;}} return W ;}// main function void main () {char s [N]; char T1 [T]; char T2 [T]; int I; int m; char * P; P = NULL; printf ("enter a string (less than 20 characters ):"); scanf ("% s", S); printf ("Enter the substring to be replaced (only two characters):"); scanf ("% s", T1 ); printf ("Enter the character string to be replaced (only two characters):"); scanf ("% s", T2); P = fun (S, T1, T2, & M); for (I = 0; I <m; I ++) // copy the returned value to the array to be output {s [I] = P [I];} printf ("Result:"); for (I = 0; I <m; I ++) // output result {printf ("% C", s [I]);} printf ("\ n ");}

 

Question 3:

# Include <stdio. h> # include <stdlib. h> # include <string. h> # define N 30 /********************************* * ************** Function Name: fun * Creation Time: 2010.12.6 * OPERATOR: huangliangming * Description: delete characters with odd ASCII values in the strings referred to by S, the remaining characters in the string form a new string and place it in the array specified by T. * Parameter: char * s, int * m * return value: Return T, (t indicates the pointer to the array) * local variable: char * t; * char temp [N]; * int I, j = 0; **************************************** * ******/char * Fun (char * s, int * m) {char * t; char temp [N]; // a temporary array used to store int I, J = 0; t = temp; for (I = 0; I <n; I ++) {If (s [I] % 2 = 0) {temp [J] = s [I]; // if it is an even number, assign the value to the array J ++ pointed by T; If (s [I] = '\ 0 ') // find the m value. First, you need to find the number of characters {break ;}} * m = J; // find the m value, that is, the length of the output result t [J] = '\ 0'; return t;} // main function void main () {char s [N]; int I; int m; char * P; P = NULL; printf ("enter a string (less than 30 characters): \ n"); scanf ("% s ", s); P = fun (S, & M ); // At the beginning, this m was not defined to limit the length of the array indicated by the pointer P. The following two characters are garbled for (I = 0; I <m; I ++) // copy the returned value to the array to be output {s [I] = P [I];} printf ("Result:"); for (I = 0; I <m; I ++) // output result {printf ("% C", s [I]);} printf ("\ n ");}

 

if you have any questions, let's discuss them.

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.