C language string operation function implementation, C language string Function

Source: Internet
Author: User
Tags string to number

C language string operation function implementation, C language string Function

1. String inversion-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. Copy strings-strcpy

char *strcpy(char *strDest, const char *strStr){    assert((NULL != strDest) && (NULL != strStr));    char *Dest=strDest;    while((*Dest++)=(*strStr++))    {}    return strDest;}

 

3. String concatenation-strcat

char *strcat(char *strDest, const char *strStr){    assert((NULL != strDest) && (NULL != strStr));    int length=strlen(strDest);    char *Dest=strDest+length;    while((*Dest++)=(*strStr++))    {}    return strDest;}

 

4. String comparison-strcmp

int strcmp(const 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(const char *strStr){    assert(NULL != strStr);    int length = 0;    while('\0' != *strStr)    {        length++;        strStr++;    }    return length;}

 

6. Convert string to number-atoi

int atoi(const char *strStr){    assert(NULL != strStr);    int minus = 0;    int begin = 0;    int sum = 0;    while('\0' !=*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*10 + (*strStr-'0');        }        strStr++;    }    return minus ? (-sum):(sum);}

 

7. convert numbers to strings-atoi

char *itoa(int num){    int temp,i,j;    char array[10];    static char strDest[10];    for(temp = num,i=0; i<10 && temp ;i++)    {        array[i]=temp%10+'0';        temp/=10;    }    for(i=i-1,j=0 ; i>=0 && j<10; i--,j++)    {        strDest[j]=array[i];    }    strDest[j]='\0';    return strDest;}

 

8. Calculate the number of metainfo characters in a string.

#include<stdio.h>#include<stdlib.h>#include<assert.h>#include<ctype.h>int isVowel(char letter){    switch(toupper(letter))    {        case 'A':        case 'E':        case 'I':        case 'O':        case 'U':            return 1;        default:            return 0;    }}int countVowel(const char * strStr){    assert(NULL != strStr);    int count=0;    while('\0' !=*strStr++)    {        if(isVowel(*strStr))            count++;    }    return count;}int main(){    char a[10]="hwlulr";    printf("%d\n",countVowel(a));    return 0;}

 

9. Determine whether a string is a return object.

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <assert. h> # include <ctype. h> int isEqual (char a, char B) {if (a = B) return 1; return 0;} int isPalindrome (const char * strStr) {assert (NULL! = StrStr); int length = strlen (strStr); int I, j; for (I = 0, j = length-1; I <j; I ++, j --) {/* Skip spaces and symbols */while (''= * (strStr + I) |! Isalpha (* (strStr + I) I ++; while (''= * (strStr + j) |! Isalpha (* (strStr + j) j --; if (0 = isEqual (* (strStr + I), * (strStr + j) return 0 ;} return 1;} int main () {char a [10] = "heo o, e h"; printf ("% s \ n", isPalindrome ()? "Is Palindrome": "is not Palindrome"); return 0 ;}

Related Article

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.