C string operation functions
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
/*
* Program function: 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 \ n", s, is_palindrome (s )? "Is a palindrome! ":" Is not a palindrome! ");
Return 0;
}