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

C语言字符串操作函数

1. 字符串反转 - strRev2. 字符串复制 - strcpy3. 字符串转化为整数 - atoi4. 字符串求长 - strlen
5. 字符串连接 - strcat6. 字符串比较 - strcmp
7. 计算字符串中的元音字符个数
8. 判断一个字符串是否是回文

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

版本1 - while版


void strRev(char *s)
{
    char temp, *end = s + strlen(s) - 1;
    while( end > s)
    {
        temp = *s;
        *s = *end;
        *end = temp;
        --end;
        ++s;
    }
}

版本2 - for版


void strRev(char *s)
{
    char temp;
    for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
    {
        temp = *s;
        *s = *end;
        *end = temp;
    }
}

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


void strRev(char *s)
{
    for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
    {
        *s ^= *end;
        *end ^= *s;
        *s ^= *end;
    }
} 

版本4 - 重构版本3


void strRev(char *s)
{
    for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
    {
        *s ^= *end ^= *s ^= *end;
    }
}

版本5 - 重构版本4

void strRev(char *s)
{
    for(char *end = s + strlen(s) - 1; end > s ; *s++ ^= *end ^= *s ^= *end--);
}

版本6 - 递归版


void strRev(const char *s)
{
    if(s[0] == '\0')
        return;
    else
        strRev(&s[1]);
    printf("%c",s[0]);
}

2. 实现库函数strcpy的功能

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

版本1


strcpy(char * dest, const char * src)
{
    char *p=dest;
    while(*dest++ = *src++)
        ;
    dest=p;
}

版本2


char * __cdecl strcpy(char * dst, const char * src)
{
    char *p = dst;
    while( *p ++ = *src ++ )
        ;
    return dst;
}

版本3


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 - 附说明


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++) //找到第一个非空字符
        ;
    if( *s >'9' || *s <'0' ) //如果第一个非空字符不是数字字符,返回0
        return 0;
    
    for(t=s; *t >='0' && *t <='9'; ++t) //找到第一个非数字字符位置 - 方法1
        ;
    t--;     /* 找到第一个非数字字符位置 - 方法2
    t=s;
    while(*t++ >='0' && *t++ <='9')
        ;
    t -= 2;
    */     while(t>=s)
    {
        n+=(*t - 48)*power(10, exp); //数字字符转化为整数
        t--;
        exp++;
    }
    return n;
}

版本2


int __cdecl atoi(const char *s)
{
    int exp=0, n=0;
    const char *t = NULL;
    
    for(; *s == ' ' || *s == '\t' || *s == '\n'; s++) //略过非空字符
        ;
    if( *s >'9' || *s <'0' )
        return 0;
    
    for(t=s; *t >='0' && *t <='9'; ++t)
        ;
    t--;     while(t>=s)
    {
        n+=(*t - 48)*pow(10, exp);
        t--;
        exp++;
    }
    return n;
}

4. 实现库函数strlen的功能

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

版本1 - while版


size_t  __cdecl strlen(const char * s)
{
    int i = 0;
    while( *s )
    {
        i++;
        s++;
    }
    return i;
}

版本2 - for版

size_t  __cdecl strlen(const char * s)
{
    for(int i = 0; *s; i++, s++)
        ;
    return i;
}

版本3 - 无变量版


size_t  __cdecl strlen(const char * s)
{
    if(*s == '\0')
        return 0;
    else
        return (strlen(++s) + 1);
}

版本4 - 重构版本3

size_t  __cdecl strlen(const char * s)
{
    return *s ? (strlen(++s) + 1) : 0;
}

5. 实现库函数strcat的功能

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

版本1 - while版


char * __cdecl strcat(char * dst, const char * src)
{
    char *p = dst;
    while( *p )
        p++;
    while( *p ++ = *src ++ )
        ;
    return dst;
}

6. 实现库函数strcmp的功能

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

版本1 - 错误的strcmp


int strcmp(const char * a, const char * b)
{
    for(; *a !='\0' && *b !='\0'; a++, b++)
        if( *a > *b)
            return 1;
        else if ( *a==*b)
            return 0;
        else
            return -1;
}

版本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. 计算字符串中元音字符的个数


#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] == '\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


/*
 * 程序功能:判断一个单词,或不含空格、标点符号的短语是否为回文(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=0; break;
        } /* 在s<=end的条件下,只要出现不相等就判断s不是回文 */
    }
    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!": "is not a palindrome!");
    return 0;
}

有趣的回文: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
Are 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 are 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
Do geese see God?
God saw I was dog
Dennis sinned

世界之最:世界上最长的回文包含了17,259个单词

说明:__cdecl,__stdcall是声明的函数调用协议.主要是传参和弹栈方面的不同.一般c++用的是__cdecl,windows里大都用的是__stdcall(API)

C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文的更多相关文章

  1. 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文

    转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 C++常用库函数atoi,itoa,strcpy,strcmp的实现 作者:jcsu C语言字符串操作函数 1. ...

  2. C语言字符串操作函数总结

    转载来源:https://blog.csdn.net/qq_33757398/article/details/81212618 字符串相关操作头文件:string.h 1.strcpy函数 原型:st ...

  3. C语言字符串操作函数整理

    整理C语言字符串相关的函数,以程序方式验证,以注释方式做说明. #include<stdio.h> #include<string.h> #include<stdlib. ...

  4. C语言字符串操作函数

    1.函数名: stpcpy  功  能: 拷贝一个字符串到另一个  用  法: char *stpcpy(char *destin, char *source);  程序例: #include < ...

  5. C语言字符串操作函数实现

    1.字符串反转 – strRev void strRev(char *str) { assert(NULL != str);   int length=strlen(str); ; while(end ...

  6. C语言-字符串操作函数

    gets(char buffer[]) 从标准输入读取一行, 并去掉换行符, 在字符串末尾增加 '\0' 字符, 写入到缓冲区 成功则返回 buffer 的地址, 出错或者遇到文件结尾则返回空指针, ...

  7. c语言字符串操作大全

     C语言字符串操作函数 函数名: strcpy 功  能: 拷贝一个字符串到另一个 用  法: char *stpcpy(char *destin, char *source); 程序例: #incl ...

  8. 实现字符串函数,strlen(),strcpy(),strcmp(),strcat()

    实现字符串函数,strlen(),strcpy(),strcmp(),strcat() #include<stdio.h> #include<stdlib.h> int my_ ...

  9. C语言的常用字符串操作函数(一)

    一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这 ...

随机推荐

  1. Python解释器的配置

    1.准备工作 安装好Pycharm2017版本 电脑上安装好Python解释器 2.本地解释器配置 配置本地解释器的步骤相对简洁直观: (1)单击工具栏中的设置按钮. (2)在Settings/Pre ...

  2. POJ - 3585 树上最大流 换根法

    题意:给出一棵树,边上有容量限制,求以任一点作为根和源点,叶子作为汇点的最大流的最大值 首先上网络流等于找死 树形DP可以\(O(n)\)求出以某点\(u\)为根的最大流,只需设\(f[u]=\sum ...

  3. 获取浏览器用户代理(user-agent)

    获取浏览器用户代理(user-agent) 用户代理(user-agent)是浏览器客户端与服务器交互时的重要信息之一,用于帮助网站识别请求用户的浏览器类别,以便于网站发送相应的网页数据. 用户代理数 ...

  4. wordpress 后台页面无法显示绑定的台湾语言

    问题:当前切换到的语言是English,然后在页面的列表中,分别显示的语言有中文和香港,没有出现台湾的图标,如上图所示 原因:在polylang插件的设置里面,可以看到台湾语言的 Language c ...

  5. Macaca 等待机制

    看代码注释todo 写博客 服务写脚本开吧 , 因为窗口太多,  不知道要去哪关闭服务 开的话无所谓 , 哪里都能开 要确认是否有开 , 直接跑代码 下面的要先过 别人的环境 工具软件自己的问题 不支 ...

  6. lucene原理

    lucene查找原理: https://yq.aliyun.com/articles/581877

  7. Oracle 如何修改列的数据类型

    链接:http://www.cnblogs.com/david-zhang-index/archive/2012/04/10/2441015.html 对字段操作 操作方法 更新字段名 alter t ...

  8. Request.QueryString 的用法

    比如常见的URL网页地址都有 xxx.asp?type=reLogin   ?号后面的就是querystring querystring是asp中获取数据的一个方法. 那么就可以用request.qu ...

  9. (转)nmon和nmon analyser的下载和使用

    原文:https://blog.csdn.net/a7442358/article/details/50186283 nmon 工具可以为 AIX 和 Linux 性能专家提供监视和分析性能数据的功能 ...

  10. oracle 中关于null的操作

    空值 空值一般用NULL表示 一般表示未知的.不确定的值,也不是空格 一般运算符与其进行运算时,都会为空 空不与任何值相等 表示某个列为空用:IS NULL  不能使用COMM=NULL这种形式 某个 ...