Tag: String
StringIt is a common data structure that can process strings flexibly. Therefore, strings are widely used in actual development, especially in non-numeric processing. Although many string operations are encapsulated in the function library, the application can directly call the library function to implement string processing. However, if developers can understand its underlying implementation principles, it is of great benefit for application programming.
This article mainly introduces the implementation principles of several common string processing functions.
I. strlen Functions
Strlen function: calculates the actual length of a string, excluding '\ 0 '.
Algorithm: starts scanning from the first character until the first '\ 0' is met, stops scanning, and returns the string length.
The Code is as follows:
int strlen(const char *str){ int n=0; assert(str!=NULL); while(*str++!='\0') ++n; return n;}
Ii. strcat Functions
Strcat function: add the source string str2 to the end of the target string str1, overwrite the '\ 0' at the end of str1, and add' \ 0' at the end of the new str1 ', returns the pointer to str1.
Algorithm: Scan str1 until '\ 0' is met. Add str2 to the end of str1 one by one, and then add' \ 0 '.
The Code is as follows:
char *strcat(char *str1, const char *str2){char *p=str1;assert( (str1!=NULL) && (str2!=NULL) ); while(*str1!='\0')str1++;while(*str1++=*str2++);return p;}
Iii. strcmp Functions
Strcmp function: Compare the size of str1 and str2 strings. If str1> str2, return a positive number. If str1 <str2, return a negative number. If str1 = str2, return 0.
Algorithm: Compare each character of str1 and str2 one by one. If the character is equal and the character '\ 0' is not met, the next character is compared. Otherwise, the difference between * str1 and * str2 is returned.
The Code is as follows:
int strcmp(const char *str1, const char *str2){assert( (str1!=NULL) && (str2!=NULL) );while(*str1 && *str2&& (*str1==*str2)) {str1++;str2++;}return *str1 - *str2;}
Iv. strcpy Functions
Strcpy function: Copy str2 (including null) to str1, and return a pointer to str1.
Algorithm: add characters in str2 to the address space pointed to by str1 one by one. Ensure that the address space pointed to by str1 is large enough.
The Code is as follows:
char *strcpy(char *str1, const char *str2) {char *p=str1;assert( (str1!=NULL) && (str2!=NULL) );while(*str1++=*str2++);return p; }
V. atoi Functions
Atoi function: converts a string to an integer.
Algorithm: Skip spaces or tabs, judge the symbols, and then subtract '0' to convert integers. Skip non-numeric values and return the converted integers.
The Code is as follows:
int atoi(char *str){int sum=0,sign=1;char *p=str;assert(str!=NULL);if(' '==*p||'\t'==*p)p++;if('-'==*p)sign=-1;if('-'==*p||'+'==*p)p++;while(*p>='0' && *p<='9'){sum = sum*10 + *p-'0';p++;}return sign*sum;}
Vi. ITOA Functions
ITOA function: converts an integer to a string.
Algorithm: first judge the integer symbol. If it is negative, it is converted to positive. The integers are stored in the TMP temporary array in sequence from one bit to the highest bit. If it is a negative integer, then, add a negative number. Put the elements of the temporary array in the STR character array in reverse order, and add an empty character at the end.
The Code is as follows:
void itoa(int num, char str[]){int i=0,j=0,sign=num;char tmp[10];if(num<0)num=-num;do{tmp[i++]=num%10 + '0';num/=10;}while(num>0);if(sign<0)tmp[i++]='-';tmp[i]='\0';i--;while(i>=0){str[j]=tmp[i];j++;i--;}str[j]='\0';}
Also: replace substrings
In many cases, you may encounter the requirement to replace the substring in the string. Several issues need to be considered:
1. Overflow
If the question needs to be replaced with the original string, consider whether the length of the string after replacement will be greater than the set length. If you can create a new string, you can set the size by yourself, in this case, there is generally no overflow problem.
2. Comparison of the length of the substring to be replaced and the length of the substring to be replaced will eventually affect the Length Change of the substring.
The following is an example:
Question: implement a function to replace each space in the string with "% 20". For example, if "We are happy." is entered, "We % 20are % 20happy." Is output .".
Solution 1: Create a new string to store the replaced string;
The Code is as follows:
# Include <stdio. h> int main () // directly implement the main function {char STR [20] = "we are happy. ", str1 [30] =" \ 0 "; char * P = STR; int I = 0; while (* P! = '\ 0') {If (* P! = '') Str1 [I ++] = * P ++; else {str1 [I ++] = '%'; str1 [I ++] = '2 '; str1 [I ++] = '0'; P ++ ;}} printf ("str1 = % s \ n", str1); Return 0 ;}
Solution 2: replace the original string with spaces from the back to the front;
The Code is as follows:
Void replaceblank (char string [], int length) {If (string = NULL & length <= 0) return; /* originallength is the actual length of string */INT originallength = 0; int numberofblank = 0; int I = 0; while (string [I]! = '\ 0') {++ originallength; If (string [I] = '') ++ numberofblank; ++ I ;} /* newlength is the length after the space is replaced with '% 20' */INT newlength = originallength + numberofblank * 2; If (newlength> length) return; int indexoforiginal = originallength; int indexofnew = newlength; while (indexoforiginal> = 0 & indexofnew> indexoforiginal) {If (string [indexoforiginal] = '') {string [indexofnew --] = '0 '; string [indexofnew --] = '2'; string [indexofnew --] = '%';} else {string [indexofnew --] = string [indexoforiginal];} -- indexoforiginal ;}}Wednesday, July 09,201 4