Turn from: C-string manipulation functions-strcpy, strcmp, strcat, inversion, palindrome
Jcsu
C-language string manipulation functions
1. String reversal-Strrev
2. String copy-strcpy
3. String conversion to integer-atoi
4. String Length-strlen
5. String connection-strcat
6. String comparison-strcmp
7. Count the number of vowel characters in a string
8. Determine if a string is a palindrome
1. Write a function to implement string inversion
Version 1-while Edition
void Strrev (char *s)
{
Char Temp, *end = s + strlen (s)-1;
while (end > s)
{
temp = *s;
*s = *end;
*end = temp;
--end;
++s;
}
}
Version 2-for Edition
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-Do not use 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-refactoring version 3
void Strrev (char *s)
{
for (char *end = s + strlen (s)-1; end > S;--end, ++s)
{
*s ^= *end ^= *s ^= *end;
}
}
Version 5-Refactoring 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] = = ' + ')
Return
Else
Strrev (&s[1]);
printf ("%c", S[0]);
}
2. Function of library function strcpy
strcpy function in 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)! = ' + '; i++)
* (dest+i) = * (Src+i);
* (dest+i) = ' + ';
}
3. Function of library function atoi
atoi function in header file <stdlib.h>
Version 1-attached instructions
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 first non-null character
;
if (*s > ' 9 ' | | *s < ' 0 ')//If the first non-null character is not a numeric character, return 0
return 0;
for (t=s; *t >= ' 0 ' && *t <= ' 9 '; ++t)//Find first non-numeric character position-Method 1
;
t--;
/* Find first non-numeric character position-Method 2
T=s;
while (*t++ >= ' 0 ' && *t++ <= ' 9 ')
;
T-= 2;
*/
while (T>=s)
{
n+= (*t-48) *power (exp); converting 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 (exp);
t--;
exp++;
}
return n;
}
4. Function of library function strlen
strlen function in header file <string.h>
Version 1-while Edition
size_t __cdecl strlen (const char * s)
{
int i = 0;
while (*s)
{
i++;
s++;
}
return i;
}
Version 2-for Edition
size_t __cdecl strlen (const char * s)
{
for (int i = 0; *s; i++, s++)
;
return i;
}
Version 3-no variable version
size_t __cdecl strlen (const char * s)
{
if (*s = = ' + ')
return 0;
Else
Return (strlen (++s) + 1);
}
Version 4-refactoring version 3
size_t __cdecl strlen (const char * s)
{
Return *s? (Strlen (++s) + 1): 0;
}
5. Function of library function strcat
strcat function in header file <string.h>
Version 1-while Edition
char * __cdecl strcat (char * DST, const char * src)
{
char *p = DST;
while (*P)
p++;
while (*p + + = *src + +)
;
return DST;
}
6. Function of library function strcmp
strcmp function in header file <string.h>
Version 1-Error strcmp
int strcmp (const char * A, const char * b)
{
for (; *a! = ' && *b! = '); 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. Count the number of vowel characters in a string
#include <stdio.h>
int Is_vowel (char a)
{
Switch (a)
{
Case ' A ': Case ' a ':
Case ' E ': Case ' e ':
Case ' I ': Case ' I ':
Case ' O ': Case ' o ':
Case ' u ': Case ' u ':
return 1; Break
Default
return 0; Break
}
}
int Count_vowel (const char *s)
{
int num;
if (s[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. Determine whether a string is a palindrome: a phrase that contains a word or does not contain spaces or punctuation. such as: Madam I ' m Adam is a palindrome
Version 1
/*
* Program function: Judge a word, or whether the phrase without spaces, punctuation is a palindrome (palindrome)
*/
#include <stdio.h>
#include <ctype.h>
int is_palindrome (const char *s)
{
BOOL is_palindrome=0;
const char *end=s;
if (*end = = ') */* If S is an empty string, it is a palindrome */
is_palindrome=1;
while (*end) ++end; /* end point to string s last character position */
--end;
while (S<=end)
{
while (*s== ' | | |!isalpha (*s)/* Omit non-alphabetic characters in string s */
++s;
while (*end== ' | | |!isalpha (*end))
--end;
if (ToUpper (*s) = = ToUpper (*end))//* Convert the alphabetic characters in s to large characters.
{
++s;
--end;
}
Else
{
is_palindrome=0; Break
}/* In s<=end conditions, as long as the unequal to Judge S is not a palindrome */
}
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!": "Are not a palindrome!");
return 0;
}
Interesting palindrome: He lived as a devil, eh?
Don ' t nod
DOGMA:I am God
Never odd or even
Too Bad–i hid a boot
Rats live on No Evil star
No Trace; Not one carton
Was it Eliot ' s toilet I saw?
Murder for a jar of Red rum
May a moody baby Doom a yam?
Go hang a salami; I ' m a lasagna hog!
Satan, oscillate my metallic sonatas!
A toyota! Race fast ... safe car:a Toyota
Straw? No, too stupid a fad; I put soot on warts
Is we not drawn onward, we few, drawn onward to new era?
Doc note:i dissent. A fast never prevents a fatness. I Diet on Cod
No, it never propagates if I set a gap or prevention
Anne, I vote more cars race Rome to Vienna
Sums is not set as a test on Erasmus
Kay, a red nude, peeped under a yak
Some men interpret nine memos
Campus motto:bottoms up, Mac
Go deliver a dare, vile dog!
Madam, in Eden I ' m Adam
Oozy rat in a sanitary zoo
Ah, Satan sees Natasha
Lisa Bonet ate no basil
Does geese see God?
God saw I was dog
Dennis sinned
World's largest: the world's longest palindrome contains 17,259 words
Description:__cdecl,__stdcall is a declared function call protocol. The main is the difference between the transfer and the stack. General C + + is used in __cdecl,windows most of the __stdcall (API)
Tags: strcmp function, strcmp, strcpy function, strcpy, C language, C language pen question, string inversion
Go: C-string manipulation functions-strcpy, strcmp, strcat, inversion, palindrome