码迷,mamicode.com
首页 > Web开发 > 详细

php函数源代码 C编写 【持续更新】

时间:2017-08-20 16:51:40      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:amp   只读   返回   clu   ram   efi   成功   strong   方式   

strlen()

获取字符串长度,成功则返回字符串 string 的长度;如果 string 为空,则返回 0。

#include<stdio.h>
#include<stdlib.h>
#define N 1000
int count = 0;

int strlen(char *str)
{
    int num = 0;                    //定义一个计数器
    while(\0 != *str++)
    {
        num++;
    }
    return num;
}

void test(char *str)
{
    printf("所要测试的字符串为: %s\n",str);
    count = strlen(str);                            //调用函数
    printf("所输入的字符串长度为:%d\n\n",count);
}

void  main()
{
    char str1[] = "hello world!";    //这样的赋值方式会有在尾部自动一个‘\0‘
    char *str2 = "hello world!";        //这样的赋值方式会有在尾部自动一个‘\0‘
    char str3[20] = "world hello!";   //这样的赋值方式会在剩余的位置全部自动添加‘\0‘
    char str4[N] = {0};
    test(str1);
    test(str2);
    test(str3);

    printf("请输入所要测试的数组:\n");
    gets(str4);            //此函数会在最后添加NULL字符 即‘\0‘
    test(str4);
system(
"pause"); }

 

strcpy()

head.h

#include<stdio.h>
#include<string.h>
#define N 100
void strcpy1(char *str_cpy, char const *str);

_strcpy().c

 

#include"head.h"


void strcpy1(char *str_cpy,char const *str)  //为了保证主数组的只读性,所以加"const"修饰
{
    while(*str != \0)
    {
        *str_cpy = *str ;
        str_cpy ++;
        str++;
    }
    *str_cpy = \0;       //添加结束符
}

 

main.c

 

#include"head.h"

void main()
{
    char str[N];
    char str_cpy[N] ;
    printf("请输入所要主字符串数组:\n");
    scanf("%s",&str);

    strcpy1(str_cpy,str);      //复制
    printf("复制前的主字符串为的%s\n",str);
    printf("复制后新字符串为的%s\n",str_cpy);

    getchar();
    getchar();
}

 

php函数源代码 C编写 【持续更新】

标签:amp   只读   返回   clu   ram   efi   成功   strong   方式   

原文地址:http://www.cnblogs.com/zhenghongxin/p/6731827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!