字符串反转,判断回文字符串,atoi,strlen


字符串反转my_strRev.c:

/*********************************************************************************
 *      Copyright:  (C) 2015 songyong<handy_skyoutlook.com>
 *                  All rights reserved.
 *
 *       Filename:  strRev.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2015年09月18日)
 *         Author:  sky <handy_sky@outlook.com>
 *      ChangeLog:  1, Release initial version on "2015年09月18日 14时06分34秒"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <string.h>

/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    char *strRev(char *s);
    char str[] = "abcdefg";

    printf("str :%s\n",str);
    //strRev(str);
    printf("after change, str:%s\n",strRev(str));
    return 0;
} /* ----- End of main() ----- */

char *strRev(char *s)
{
    char temp;
    char *front = s;
    char *end = s + strlen(s)-1;

    while(end > s)
    {
        temp = *s;
        *s = *end;
        *end = temp;
        --end;
        ++s;
    }
    return front;
}


实现strlen函数,my_strlen.c:

/*********************************************************************************
 *      Copyright:  (C) 2015 songyong<handy_skyoutlook.com>
 *                  All rights reserved.
 *
 *       Filename:  my_strlen.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2015年09月18日)
 *         Author:  sky <handy_sky@outlook.com>
 *      ChangeLog:  1, Release initial version on "2015年09月18日 15时59分49秒"
 *                 
 ********************************************************************************/
#include <stdio.h>
/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    char a[] = "1234";
    printf("a[] = %d\n",my_strlen(a));
    return 0;
} /* ----- End of main() ----- */

int my_strlen(char *s)
{
    int n = 0;
    while(*s)
    {
        n++;
        s++;
    }
    return n;
}


实现atoi函数,my_atoi.c:

/*********************************************************************************
 *      Copyright:  (C) 2015 songyong<handy_skyoutlook.com>
 *                  All rights reserved.
 *
 *       Filename:  atoi.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2015年09月18日)
 *         Author:  sky <handy_sky@outlook.com>
 *      ChangeLog:  1, Release initial version on "2015年09月18日 14时38分15秒"
 *                 
 ********************************************************************************/
#include<stdio.h>

#define TEN 10
/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    char str[] = "123...hello world!";
    printf("str atoi :%d\n",atoi(str));
    return 0;
} /* ----- End of main() ----- */

int power(int base,int exp)
{
    if(exp == 0)
        return 0;
    return base*power(base,exp-1);
}

int my_atoi(const char *s)
{
    int n = 0;
    int exp = 0;
    const char *t = NULL;

    for( ;*s ==' '|| *s == '\t'|| *s == '\n';s++)//首先找到第一个非空字符;
        ;
    if( *s < '0' && *s > '9')//判断是否是数字字符;
        return 0;
    for(t = s; *t >= '0' && *t <= '9'; ++t)//找到这串数字字符后的第一个非数字字符;
        ;
    t--;//最后一个数字字符的位置;
    while(t >= s)
    {
        n += (*t-48)*power(TEN,exp);//1的ASIIC为1,’1‘的ASIIC为49;将原来数字字符串转换后的值换算成整数;
        t--;
        exp++;
    }
    return n;
}


判断是否是回文:

/*********************************************************************************
 *      Copyright:  (C) 2015 songyong<handy_skyoutlook.com>
 *                  All rights reserved.
 *
 *       Filename:  huiwen.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2015年09月13日)
 *         Author:  sky <handy_sky@outlook.com>
 *      ChangeLog:  1, Release initial version on "2015年09月13日 14时40分53秒"
 *                 
 ********************************************************************************/
#include<stdio.h>
#include<string.h>

#define LEN 100
/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    char str[LEN];
    int len, flag, i;

    while(scanf("%s",str) != EOF)
    {
        flag = 1;
        len = strlen(str);
        for(i =0; i< len/2; i++)
        {
            if(str[i] != str[len - i - 1])//通过指向首尾两个位置的指针相向移动进行比较,所对应的值不相同则不为回文。
            {
                flag = 0;
                break;
            }
        }
        if(flag)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
} /* ----- End of main() ----- */













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值