Due to academic reasons, this section has recently been in contact with C, and the process orientation is a little different from the object-oriented approach.
However, the method can still be written as a public one... the following functions are often used...
/* 25 **************** * ******************************** sourcestr source string, desstr judge string *************************/INT stringjudge (char sourcestr [], char desstr []) {int Results = 0, I; I = strlen (desstr);/* Get the length of the substring */while (sourcestr = strstr (sourcestr, desstr ))! = NULL)/* Find the first occurrence of the string in the string */{ Sourcestr = sourcestr + I;/* Move back */ Results ++;} return results ;} /* 26 ****************** determine whether the input parameter type is correct ************* * *************/INT judgeinputdata (char * STR, int status ){ Int I = 0; Int Len = strlen (STR ); Int boolpoint = 0; Switch (Status) { Case isnum: For (I = 0; I <Len; I ++) { If (! (STR [I]> = 48 & STR [I] <= 57 )) { Return 0; } } Return 1; Break; Case ischar: For (I = 0; I <Len; I ++) { If (! (STR [I]> = 65 & STR [I] <= 90) | (STR [I] >=97 & STR [I] <= 122 ))) { Return 0; } } Return 1; Break; Case isfloat: For (I = 0; I <Len; I ++) { If (STR [I]> = 48 & STR [I] <= 57) | (STR [I] = '.' & boolpoint = 0 )) { If (STR [I] = '.') { Boolpoint ++; } Continue; } Return 0; } Return 1; Break; } Return 1 ;} /* 27 ***************** enter a parameter and perform the judgment operation *********** * **************** // * parameters: * ****************** STR (input character), Len (input character length), * printinfo (input character prompt ), status (input parameter type), max (maximum input value Limited) * *************************/void inputdata (char * STR, int Len, char * printinfo, int status, int max ){ Char ch [1024] = {0 }; Char Buf [1024] = {0 }; Int I = 0; Memset (STR, 0, sizeof (STR )); Printf (printinfo ); // Print the prompt for the user to enter Sprintf (BUF, "incorrect input. Please try again! \ N % s ", printinfo ); Switch (Status) { Case isnum: While (1) { Scanf ("% s", CH ); Strncpy (STR, CH, Len ); If (judgeinputdata (CH, status) = 1) { If (atoi (STR)> MAX ){ Sprintf (BUF, "input exceeds the range of % d. Please enter it again! \ N % s ", Len, printinfo ); } Else Break; } Printf (BUF ); // Print the error message so that the user can enter it again } Break; Case ischar: While (1) { Scanf ("% s", CH ); If (judgeinputdata (CH, status) = 1) { Break; } Printf (BUF ); // Print the error message so that the user can enter it again } Strncpy (STR, CH, Len ); Break; Case isfloat: While (1) { Scanf ("% s", CH ); Strncpy (STR, CH, Len ); If (judgeinputdata (CH, status) = 1) { If (atoi (STR)> MAX ){ Sprintf (BUF, "input exceeds the range of % d. Please enter it again! \ N % s ", Max, printinfo ); } Else Break; } Printf (BUF ); // Print the error message so that the user can enter it again } Break; Default: Break; }
}