php函數源代碼 C編寫 【持續更新】
阿新 • • 發佈:2017-08-20
字符串 itl 自動 code strcpy return div 取字符 pau
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, charconst *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編寫 【持續更新】