標籤: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編寫 【持續更新】