C-Language string manipulation function implementation

Source: Internet
Author: User
Tags strcmp

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.