How to Write strcpy, strcmp, strcat, and strlen Functions

Source: Internet
Author: User
VC source code: strcmp function writing: # include <stdio. h> # include <string. h> int strcmp1 (char * a, char * B) {for (; * A = * B; A ++, B ++) if (*! = '\ 0') return 0; return * A-* B;} Main () {int L; char a [10] = {"DB "}; char B [10] = {"CB"}; L = strcmp1 (a, B); printf ("% d \ n", L );}
Strcat function Syntax: # include <stdio. h> # include <string. h> char * strcat1 (char * DST, char * SRC) {char * CP = DST; while (* CP) CP ++; /* Find end of DST */while (* CP ++ = * SRC ++);/* Copy SRC to end of DST */Return (DST ); /* return DST */} Main () {char a [10] = {"wangefng"}; char B [10] = {"junzi"}; strcat1 (, b); puts (a);} strcpy function Syntax: # include <stdio. h> # include <string. h> char * strcpy1 (char * To, char * From) {char * P = to; while (* to + + = * From ++); Return P;} Main () {char a [10] = {"wangefng"}; char B [10] = {"junzi "}; strcpy1 (a, B); puts (a);} strcat Function Array Implementation: # include <stdio. h> void strcat1 (char s [], char T []) {Int J = 0, I = 0; while (s [I]! = '\ 0') // determines whether it is the end of string s I ++; while (S [I] = T [J])! = '\ 0') I ++, J ++;} Main () // main function, function used to test the strcat function {char s [20] = "I love"; // The character array S should be defined with sufficient length, to load the connected string char T [] = "you"; strcat1 (S, T); // call the string connection function printf ("the length of the connected string is: % s \ n ", S );
} Strcat function pointer implementation: # include <stdio. h> char * strcat1 (char * s, char * t) {While (* s) // judge whether it is the end of string s ++; while (* s ++ = * t ++) // copy the string in T to the end of S; return s;} Main () // main function, function used to test the strcat function {char a [20] = "I love"; // character array S should be defined with sufficient length, to load the connected string char B [5] = "you"; strcat1 (a, B); // call the string connection function printf ("the length of the connected string is: % s \ n ", a) ;}# include <stdio. h> void connect (char str1 [], char str2 []) {int I, j; I = strlen (str1); For (j = 0; str2 [J]! = '\ 0'; j ++) str1 [I + J] = str2 [J]; str1 [I + J] =' \ 0';} Main () {char str1 [200], str2 [100]; printf ("Please input the first string: \ n"); gets (str1); printf ("Please input the second string: \ n "); gets (str2); Connect (str1, str2); printf (" after CONNECT: \ n "); puts (str1 );}
Strlen function pointer Syntax: # include <stdio. h> # include <string. h> char strlen1 (char * s) {int I = 0; while (* s ++) ++ I; return I;} Main () {char s [10] = "Iloveyou"; printf ("String Length: % d \ n", strlen1 (s ));}

You can also use the For Loop: Char strlen1 (char * s) {int N; For (n = 0; * s! = "\ 0"; s ++) n ++; return n ;}} strlen function syntax # include <stdio. h> int strlen1 (char * s) {int n = 0; while (s [N]! = '\ 0') + + N; return N;} Main () {char s [10] = "Iloveyou"; printf ("% d \ n ", strlen1 (s ));




2. Write a function int fun (char * P) to judge whether a string is a return text. If it is 1, it is not 0. If an error is returned, it is-1.
Int fun (char * P) {int I = 0, Len = 0, flag = 0; while (* P )! = '\ 0') {P ++; Len ++;} If (LEN = 0) return flag =-1; P = p-len; for (I = 0; I <= Len/2; I ++) {If (P [I]! = P [len-1-i]) return flag = 0;} return flag = 1 ;}}
Int strcmp (const char * str1, const char * str2) {int Len = 0; while (* str1 & * str2 & (* str1 = * str2 )) {str1 ++; str2 ++;

}
Return * str1-* str2;


}

How to Write strcpy, strcmp, strcat, and strlen Functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.