zoukankan      html  css  js  c++  java
  • C语言字符串操作函数

    引用自:

    http://www.cnblogs.com/JCSU/articles/1305401.html

    1. 字符串反转 - strRev
    2. 字符串复制 - strcpy
    3. 字符串转化为整数 - atoi
    4. 字符串求长 - strlen
    5. 字符串连接 - strcat
    6. 字符串比较 - strcmp
    7. 计算字符串中的元音字符个数
    8. 判断一个字符串是否是回文

    1. 写一个函数实现字符串反转

    版本1 - while版

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->void strRev(char *s)
    {
        
    char temp, *end = s + strlen(s) - 1;
        
    while( end > s)
        {
            temp 
    = *s;
            
    *= *end;
            
    *end = temp;
            
    --end;
            
    ++s;
        }
    }


    版本2 - for版

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->void strRev(char *s)
    {
        
    char temp;
        
    for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
        {
            temp 
    = *s;
            
    *= *end;
            
    *end = temp;
        }
    }


    版本3 - 不使用第三方变量

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->void strRev(char *s)
    {
        
    for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
        {
            
    *^= *end;
            
    *end ^= *s;
            
    *^= *end;
        }


    版本4 - 重构版本3

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->void strRev(char *s)
    {
        
    for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
        {
            
    *^= *end ^= *^= *end;
        }
    }


    版本5 - 重构版本4

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->void strRev(char *s)
    {
        
    for(char *end = s + strlen(s) - 1; end > s ; *s++ ^= *end ^= *^= *end--);
    }


    版本6 - 递归版

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->void strRev(const char *s)
    {
        
    if(s[0== '\0')
            
    return;
        
    else
            strRev(
    &s[1]);
        printf(
    "%c",s[0]);
    }



    2. 实现库函数strcpy的功能

    strcpy函数位于头文件<string.h>中

    版本1

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->strcpy(char * dest, const char * src)
    {
        
    char *p=dest;
        
    while(*dest++ = *src++)
            ;
        dest
    =p;
    }


    版本2

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->char * __cdecl strcpy(char * dst, const char * src)
    {
        
    char *= dst;
        
    while*++ = *src ++ )
            ;
        
    return dst;
    }


    版本3

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->strcpy(char * dest, const char * src)
    {
        
    int i=0;
        
    for(; *(src+i)!='\0'; i++)
            
    *(dest+i) = *(src+i);
        
    *(dest+i) = '\0';
    }



    3. 实现库函数atoi的功能

    atoi函数位于头文件<stdlib.h>中

    版本1 - 附说明

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->int power(int baseint exp)
    {
        
    if0 == exp )
            
    return 1;
        
    return base*power(base, exp-1);
    }

    int __cdecl atoi(const char *s)
    {
        
    int exp=0, n=0;
        
    const char *= NULL;
        
        
    for(; *== ' ' || *== '\t' || *== '\n'; s++//找到第一个非空字符
            ;
        
    if*>'9' || *<'0' ) //如果第一个非空字符不是数字字符,返回0
            return 0;
        
        
    for(t=s; *>='0' && *<='9'++t) //找到第一个非数字字符位置 - 方法1
            ;
        t
    --;

        
    /* 找到第一个非数字字符位置 - 方法2
        t=s;
        while(*t++ >='0' && *t++ <='9')
            ;
        t -= 2;
        
    */

        
    while(t>=s)
        {
            n
    +=(*- 48)*power(10, exp); //数字字符转化为整数
            t--;
            exp
    ++;
        }
        
    return n;
    }


    版本2

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->int __cdecl atoi(const char *s)
    {
        
    int exp=0, n=0;
        
    const char *= NULL;
        
        
    for(; *== ' ' || *== '\t' || *== '\n'; s++//略过非空字符
            ;
        
    if*>'9' || *<'0' )
            
    return 0;
        
        
    for(t=s; *>='0' && *<='9'++t)
            ;
        t
    --;

        
    while(t>=s)
        {
            n
    +=(*- 48)*pow(10, exp);
            t
    --;
            exp
    ++;
        }
        
    return n;
    }



    4. 实现库函数strlen的功能

    strlen函数位于头文件<string.h>中

    版本1 - while版

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->size_t  __cdecl strlen(const char * s)
    {
        
    int i = 0;
        
    while*s )
        {
            i
    ++;
            s
    ++;
        }
        
    return i;
    }


    版本2 - for版

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->size_t  __cdecl strlen(const char * s)
    {
        
    for(int i = 0*s; i++, s++)
            ;
        
    return i;
    }


    版本3 - 无变量版

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->size_t  __cdecl strlen(const char * s)
    {
        
    if(*== '\0')
            
    return 0;
        
    else
            
    return (strlen(++s) + 1);
    }


    版本4 - 重构版本3

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->size_t  __cdecl strlen(const char * s)
    {
        
    return *? (strlen(++s) + 1) : 0;
    }



    5. 实现库函数strcat的功能

    strcat函数位于头文件<string.h>中

    版本1 - while版

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->char * __cdecl strcat(char * dst, const char * src)
    {
        
    char *= dst;
        
    while*p )
            p
    ++;
        
    while*++ = *src ++ )
            ;
        
    return dst;
    }



    6. 实现库函数strcmp的功能

    strcmp函数位于头文件<string.h>中

    版本1 - 错误的strcmp

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->int strcmp(const char * a, const char * b)
    {
        
    for(; *!='\0' && *!='\0'; a++, b++)
            
    if*> *b)
                
    return 1;
            
    else if ( *a==*b)
                
    return 0;
            
    else
                
    return -1;
    }


    版本2

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->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. 计算字符串中元音字符的个数

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#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 1break;
        
    default
            
    return 0break;
        }
    }

    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. 判断一个字符串是否回文:包含一个单词,或不含空格、标点的短语。如:Madam I'm Adam是回文

    版本1

    <!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->/*
     * 程序功能:判断一个单词,或不含空格、标点符号的短语是否为回文(palindrome)
     
    */
    #include 
    <stdio.h>
    #include 
    <ctype.h>

    int is_palindrome(const char *s)
    {
        
    bool is_palindrome=0;
        
    const char *end=s;

        
    if(*end == '\0'/* 如果s为空串,则是回文 */
            is_palindrome
    =1;

        
    while(*end) ++end; /* end指向串s最后一个字符位置 */
        
    --end;

        
    while(s<=end)
        {
            while(*s==' ' || !isalpha(*s)) /* 略去串s中的非字母字符 */
                ++s;
            while(*end==' ' || !isalpha(*end))
                --end;
            if(toupper(*s) == toupper(*end)) /* 将s中的字母字符转换为大字进行判断 */
            {
                ++s;
                --end;
            } 
            else 
            {
                is_palindrome=0break;
            } /* 在s<=end的条件下,只要出现不相等就判断s不是回文 */
        }
        if(s>end)
            is_palindrome=1;
        else
            is_palindrome=0;
        return (is_palindrome);

    }

    int main()
    {
        const char *="Madam  I' m   Adam";
        printf("%s %s \n", s, is_palindrome(s) ? "is a palindrome!""is not a palindrome!");
        return 0;
    }


    趣的He lived as a devil, eh?


  • 相关阅读:
    【记录】20060430 11:30:00 本Blog访问量突破100000 !
    ESFramework介绍之(24)―― 日志记录IEsbLogger
    ESFramework介绍之(20)―― 插件自动升级
    ESFramework介绍之(21)-- Tcp组件接口ITcp介绍
    ESFramework介绍之(17)―― 支持漫游用户和跨区域功能请求
    python标准库学习9
    python标准库学习5 bisect — Array bisection algorithm
    C++中四种显示类型转换总结
    BeautifulSoup学习笔记
    jdbc连接数据库(水文)
  • 原文地址:https://www.cnblogs.com/yuzaipiaofei/p/4124252.html
Copyright © 2011-2022 走看看