Common special operations on c strings (except those implemented by string. c)
In string operations, some of the articles that must be mastered have been written before, such as string SEARCH, string pasting, string copying, and so on. These are strings in the Standard C library. c has been implemented, so # include You can use it.
In addition to the aforementioned interfaces, there are also some commonly used interfaces at work, such as deleting the specified character from the string, deleting the first character of the string, and turning the string left and right, today, let's look at a few of these interfaces:
We directly go to the Code:
# Include
# Include
# Include
# Define uchar unsigned char # define uint unsigned int // Delete All digits in the string void del_str_Num (char * str) {static int I, j; while (str [I]! = '\ 0') {if (str [I]> '9' | str [I] <'0') str [j ++] = str [I]; I ++;} str [j] = '\ 0';} // Delete the first character in the string static char * ltrim (char * s, char c) {while (* s! = 0 & * s = c) s ++; return s ;}// find and delete any character specified in the string static char * delstr_c (char s [100], char c) {char * p, * q; for (p = s, q = s; * p! = '\ 0'; p ++) if (* p! = C) * q ++ = * p; * q = * p; return s ;} /* Delete the character c in string s */void del_str_c (char s [], int c) {int I, j; for (I = 0, j = 0; s [I]! = '\ 0'; I ++) {if (s [I]! = C) {s [j ++] = s [I] ;}} s [j] = '\ 0 ';} // search for characters in the string and delete the corresponding characters from char * strdel_c (char * s, char c) {char * p = s, * q = s; (; * p; p ++) {if (* p! = C) {if (q = p) q ++; else * q ++ = * p;} * q = '\ 0'; return s ;} // flip the left and right character strings to char * strfilp (char * str) {// h to the str header char * h = str; char * t = str; char ch; // t points to the end of s while (* t ++); t --; // offset t with t ++ --; // skip the terminator '\ 0' // when h and t are not duplicated, exchange the characters they direct to while (h <t) {ch = * h; * h ++ = * t; // start and end movement * t -- = ch;} return (str);} int main () {char str [20] = "23.3hhlo965 "; char * str1 = "hello"; del_str_c (str ,'. '); // Delete. printf ("% s \ n", str); del_str_Num (str); // Delete All digits in the string printf ("% s \ n", str ); strfilp (str); // flip the left and right sides of the string printf ("% s \ n", str); return 0 ;}
Running result:
I tested only three of the above interfaces. The rest are basically the same, but they are implemented in different ways.