1. String reversal –strrev
void strrev (char *str) { assert (NULL! = str);
int length=strlen (str); Char *end=str+length-1; while (End > str) { *str= (*str) ^ (*end); *end= (*STR) ^ (*end); *str= (*STR) ^ (*end); End--; Str+ +; }}
2. String Copy –strcpy
Char *strcpy (charconstChar *strStr) { ! = strdest) && (NULL!) = strStr)); Char *dest=strdest; while ((*dest++) = (*strstr++)) {} return strdest;}
3, String splicing –strcat
Char *strcat (charconstChar *strStr) { ! = strdest) && (NULL!) = strStr)); int length=strlen (strdest); Char *dest=strdest+length; while ((*dest++) = (*strstr++)) {} return strdest;}
4. String comparison –strcmp
intstrcmpConst Char*strdest,Const Char*strStr) {Assert (NULL! = strdest) && (NULL! =strStr)); while(0= = (*strdest-*strstr) && *strdest) {strdest++; StrStr++; } if(*strdest > *strStr)return 1; Else if(*strdest < *strStr)return-1; Else return 0;}
5. String length –strlen
int strlen (constChar *strStr) { ! = strStr) ; int 0 ; while ('strStr' ! = * * * *) { length+ +; StrStr++ ; } return length;}
6. String to Digital –atoi
intAtoiConst Char*strStr) {Assert (NULL!=strStr); intminus =0; intBegin =0; intsum =0; while(' /'!=*strStr) { if(0==begin && (isdigit (*strstr) | |'+'==*strstr | |'-'==*strStr)) {Begin=1; if('-'==*strStr) {Minus=1; StrStr++; Continue; } } Else if(!isdigit (*strStr)) {printf ("format is wrong!\n"); Exit (1); } if(1==begin) {Sum= sum*Ten+ (*strstr-'0'); } strStr++; } returnMinus? (-sum):(sum);}
7, Digital to string –atoi
Char*itoa (intnum) { inttemp,i,j; Chararray[Ten]; Static Charstrdest[Ten]; for(temp = num,i=0; i<Ten&& temp; i++) {Array[i]=temp%Ten+'0'; Temp/=Ten; } for(i=i-1, j=0; i>=0&& j<Ten; I--, J + +) {Strdest[j]=Array[i]; } Strdest[j]=' /'; returnstrdest;}
8. Count the number of vowel characters in a string
#include <stdio.h>#include<stdlib.h>#include<assert.h>#include<ctype.h>intIsvowel (CharLetter ) { Switch(ToUpper (letter)) { Case 'A': Case 'E': Case 'I': Case 'O': Case 'U': return 1; default: return 0; }}intCountvowel (Const Char*strStr) {Assert (NULL!=strStr); intCount=0; while(' /'!=*strstr++) { if(Isvowel (*strStr)) Count++; } returncount;}intMain () {Chara[Ten]="HWLULR"; printf ("%d\n", Countvowel (a)); return 0;}
9, judge whether a string is a palindrome
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<assert.h>#include<ctype.h>intTestCharACharb) { if(a==b)return 1; return 0;}intIspalindrome (Const Char*strStr) {Assert (NULL!=strStr); intLength=strlen (STRSTR); inti,j; for(i=0, j=length-1; I<j; i++,j--) { if(0==test (* (Strstr+i), * (strstr+J )))return 0; } return 1;}intMain () {Chara[Ten]="Heooeh"; printf ("%s\n", Ispalindrome (a)?"is palindrome":"is not palindrome"); return 0;}
C-Language string manipulation function implementation