C string operation functions
1. String inversion-strrev
2. Copy strings-strcpy
3. Convert the string to an integer-atoi
4. String Length-strlen
5. String connection-strcat
6. String comparison-strcmp
7. Calculate the number of vowels in a string.
8. Determine whether a string is a text return.
1. Write a function to reverse the string
Version 1-while
Void strrev (char * s)
{
Char temp, * end = S + strlen (S)-1;
While (end> S)
{
Temp = * s;
* S = * end;
* End = temp;
-- End;
++ S;
}
}
Version 2-
Void strrev (char * s)
{
Char temp;
For (char * end = S + strlen (S)-1; end> S; -- end, ++ S)
{
Temp = * s;
* S = * end;
* End = temp;
}
}
Version 3-no third-party Variables
Void strrev (char * s)
{
For (char * end = S + strlen (S)-1; end> S; -- end, ++ S)
{
* S ^ = * end;
* END ^ = * s;
* S ^ = * end;
}
}
Version 4-rebuilding Version 3
Void strrev (char * s)
{
For (char * end = S + strlen (S)-1; end> S; -- end, ++ S)
{
* S ^ = * END ^ = * s ^ = * end;
}
}
Version 5-rebuilding version 4
Void strrev (char * s)
{
For (char * end = S + strlen (S)-1; end> S; * s ++ ^ = * END ^ = * s ^ = * end --);
}
Version 6-recursive version
Void strrev (const char * s)
{
If (s [0] = '\ 0 ')
Return;
Else
Strrev (& S [1]);
Printf ("% C", s [0]);
}
2. Implement the strcpy function of the database.
The strcpy function is located in the header file <string. h>.
Version 1
Strcpy (char * DEST, const char * SRC)
{
Char * P = DEST;
While (* DEST ++ = * SRC ++)
;
DeST = P;
}
Version 2
Char * _ cdecl strcpy (char * DST, const char * SRC)
{
Char * P = DST;
While (* P ++ = * SRC ++)
;
Return DST;
}
Version 3
Strcpy (char * DEST, const char * SRC)
{
Int I = 0;
For (; * (SRC + I )! = '\ 0'; I ++)
* (DEST + I) = * (SRC + I );
* (DEST + I) = '\ 0 ';
}
3. Implement the atoi function of the library function
The atoi function is located in the header file <stdlib. h>.
Version 1-Appendix
Int power (INT base, int exp)
{
If (0 = exp)
Return 1;
Return base * power (base, exp-1 );
}
Int _ cdecl atoi (const char * s)
{
Int exp = 0, n = 0;
Const char * t = NULL;
For (; * s = ''| * s = '\ T' | * s =' \ n'; s ++) // find the first non-null character
;
If (* s> '9' | * S <'0') // if the first non-null character is not a numeric character, 0 is returned.
Return 0;
For (t = s; * T> = '0' & * t <= '9'; ++ t) // locate the first non-numeric character position-method 1
;
T --;
/* Locate the first non-numeric character location-method 2
T = s;
While (* t ++> = '0' & * t ++ <= '9 ')
;
T-= 2;
*/
While (T> = s)
{
N + = (* t-48) * power (10, exp); // convert numeric characters to Integers
T --;
Exp ++;
}
Return N;
}
Version 2
Int _ cdecl atoi (const char * s)
{
Int exp = 0, n = 0;
Const char * t = NULL;
For (; * s = ''| * s = '\ T' | * s =' \ n'; s ++) // skip non-null characters
;
If (* s> '9' | * S <'0 ')
Return 0;
For (t = s; * T> = '0' & * t <= '9'; ++ T)
;
T --;
While (T> = s)
{
N + = (* t-48) * POW (10, exp );
T --;
Exp ++;
}
Return N;
}
4. Implement the strlen function of the library function
The strlen function is located in the header file <string. h>.
Version 1-while
Size_t _ cdecl strlen (const char * s)
{
Int I = 0;
While (* s)
{
I ++;
S ++;
}
Return I;
}
Version 2-
Size_t _ cdecl strlen (const char * s)
{
For (INT I = 0; * s; I ++, s ++)
;
Return I;
}
Version 3-variable-Free Edition
Size_t _ cdecl strlen (const char * s)
{
If (* s = '\ 0 ')
Return 0;
Else
Return (strlen (++ s) + 1 );
}
Version 4-rebuilding Version 3
Size_t _ cdecl strlen (const char * s)
{
Return * s? (Strlen (++ s) + 1): 0;
}
5. Implement the strcat function of the library function
The strcat function is located in the header file <string. h>.
Version 1-while
Char * _ cdecl strcat (char * DST, const char * SRC)
{
Char * P = DST;
While (* P)
P ++;
While (* P ++ = * SRC ++)
;
Return DST;
}
6. Implement the strcmp function of the Database Function
The strcmp function is located in the header file <string. h>.
Version 1-invalid strcmp
Int strcmp (const char * a, const char * B)
{
For (; *! = '\ 0' & * B! = '\ 0'; A ++, B ++)
If (* A> * B)
Return 1;
Else if (* A = * B)
Return 0;
Else
Return-1;
}
Version 2
Int _ cdecl strcmp (const char * SRC, const char * DST)
{
Int ret = 0;
While (! (Ret = * (unsigned char *) Src-* (unsigned char *) DST) & * SRC)
++ SRC, ++ DST;
If (Ret <0)
Ret =-1;
Else if (Ret> 0)
Ret = 1;
Return (RET );
}
7. Calculate the number of metainfo characters in a string
# Include <stdio. h>
Int is_vowel (char)
{
Switch ()
{
Case 'A ':
Case 'E ':
Case 'I ':
Case 'O ':
Case 'U ':
Return 1; break;
Default:
Return 0; break;
}
}
Int count_vowel (const char * s)
{
Int num;
If (s [0] = '\ 0 ')
Num = 0;
Else
{
If (is_vowel (s [0])
Num = 1 + count_vowel (& S [1]);
Else
Num = count_vowel (& S [1]);
}
Return num;
}
Int main ()
{
Char * s = "aobcd ddudie ";
Printf ("% d \ n", count_vowel (s ));
Return 0;
}
8. judge whether a string contains a word or a phrase without spaces or punctuation. For example, Madam I'm Adam is a echo.
Version 1
/*
*ProgramFunction: determines whether a word or phrase without spaces or punctuation is palindrome)
*/
# Include <stdio. h>
# Include <ctype. h>
Int is_palindrome (const char * s)
{
Bool is_palindrome = 0;
Const char * end = s;
If (* end = '\ 0')/* If s is an empty string, it is a reply */
Is_palindrome = 1;
While (* End) + + end;/* end points to the last character position of string S */
-- End;
While (S <= end)
{
While (* s = ''|! Isalpha (* s)/* omitted non-letter characters in string S */
++ S;
While (* end = ''|! Isalpha (* End ))
-- End;
If (toupper (* S) = toupper (* End)/* converts the letter character in s to a big character for judgment */
{
++ S;
-- End;
}
Else
{
Is_palindrome = 0; break;
}/* Under the S <= end condition, if there is an inequality, the system determines that S is not a return object */
}
If (S> end)
Is_palindrome = 1;
Else
Is_palindrome = 0;
Return (is_palindrome );
}
Int main ()
{
Const char * s = "Madam I'm Adam ";
Printf ("% S % s \ n", S, is_palindrome (s )? "Is a palindrome! ":" Is not a palindrome! ");
Return 0;
}