php函數原始碼 C編寫 【持續更新】

來源:互聯網
上載者:User

標籤:amp   唯讀   返回   clu   ram   efi   成功   strong   方式   

strlen()

擷取字串長度,成功則返回字串 string 的長度;如果 string 為空白,則返回 0。

#include<stdio.h>#include<stdlib.h>#define N 1000int 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 100void 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編寫 【持續更新】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.