Php function source code C write [continuous update], php source code
Strlen ()
Returns the string if the string length is obtained.string
Length; ifstring
If it is null, 0 is returned.
# Include <stdio. h> # include <stdlib. h> # define N 1000int count = 0; int strlen (char * str) {int num = 0; // define a counter while ('\ 0 '! = * Str ++) {num ++;} return num;} void test (char * str) {printf ("the string to be tested is % s \ n ", str); count = strlen (str); // call the printf function ("the input string length is % d \ n", count);} void main () {char str1 [] = "hello world! "; // This assignment method will automatically end with a '\ 0' char * str2 =" hello world! "; // This assignment method will automatically end with a '\ 0' char str3 [20] =" world hello! "; // This assignment method will automatically add '\ 0' char str4 [N] = {0}; test (str1); test (str2 ); test (str3); printf ("Enter the array to be tested: \ n"); gets (str4 ); // This function adds the NULL character '\ 0' test (str4) at the end );
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) // to ensure read-only of the primary array, add" const "to modify {while (* str! = '\ 0') {* str_cpy = * str; str_cpy ++; str ++;} * str_cpy =' \ 0'; // Add the terminator}
Main. c
# Include "head. h "void main () {char str [N]; char str_cpy [N]; printf (" Enter the main string array: \ n "); scanf ("% s", & str); strcpy1 (str_cpy, str); // copy printf ("% s \ n", str ); printf ("% s \ n", str_cpy); getchar ();}