Rearrange a string, with the letters at the top and numbers at the bottom, without changing the character sequence between the original letters and numbers.
# Include <stdio. h> # include <string. h> # include <stdlib. h> # define N 30 /********************************* * ************** Function Name: fun * Creation Time: 2010.12.5 * Description: sorts a string in front of the letter and the number behind it, without changing the character sequence between the 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] = ''; chNumber [k] = ''; * 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 ");}
Replace the substrings that last appear in the string s with the strings that T2.
# 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 * Description: Replace the substring that is the last occurrence of the string in s with the string in t1 with the string in t2 * parameter: char * s, char * t1, char * t2, int * m * return value: w (first element address of 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] = ''; // evaluate the value referred to by m for (I = 0; I <N; I ++) {if (s [I] = '') {* 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] = ''; if (strcmp (t1temp, temp) = 0) {k = I; // subscripts 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 ");}
Delete the characters with an odd ASCII value in the string s, and place the remaining characters in the string into a new string in the array t.
# Include <stdio. h> # include <stdlib. h> # include <string. h> # define N 30 /********************************* * ************** Function Name: fun * Creation Time: 2010.12.6 * Description: Delete the characters with an odd ASCII value in the string referred to by s, and place the remaining characters in the string into a new string in the array referred to 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] = '') // 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] = ''; 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 ");}