String as a more important part of C language, learning this part requires us to understand more. Analysis. We are in the process of learning the first in the mind a clear idea, according to this idea on the computer to knock out. Just start learning string knocking out there will be a lot of mistakes, we do not give up, remember before a senior said to find the mistake is a programmer happiest, find out the wrong changes we will have a deep impression, slowly our mistakes will be less and fewer. Want to become a good programmer, we must have the patience, just start wrong more, we are serious about not let go of one, slowly mistakes will be fewer and less.
I think I learn programming, the most happy thing when knocking code is to find the wrong place and modify the success. This makes it very rare to make mistakes in the same place during the subsequent knock. This is my own learning programming some methods, hope to share out we can study together, bad hope you in the comments in the message Oh!
The next thing I want to share is my thoughts and methods of learning strcpy,strncpy,strcat,strncat,strcmp.
First talk about strcpy,strncpy.
In the main function, first define two string array char Dest[10];char src[]= "abcdef"; Copy the string from src to dest. The method is implemented by calling the function, passing in the main function two arrays, in the calling function with a pointer to receive, char* dest,char* src. Define a pointer p in the calling function to point to dest,char* p=dest, and assert if dest and SRC exist, If present, when SRC content! = "\" When the contents of SRC Copy to Dest, and at the same time dest address Gaga, src address gaga. Finally, return p.
#include <stdio.h> #include <string.h> #include <assert.h>char *my_strcpy (char *dest, const char *SRC) {Char *p = dest; assert (dest); assert (SRC);/*while (*dest++ = *src++) {;} */while (*src! = ') "{*dest = *src;src++;d est++; }*dest = *src;return p;} int main () {char Dest[10];char src[] = "ASDFG"; char *ret = my_strcpy (DEST,SRC);p UTS (ret); return 0;}
Similarly, the copy method of strncpy and strcpy is similar, except that the first few copies of the string are given to another array.
#include <stdio.h> #include <string.h> #include <assert.h>char *my_strncpy (char *asd,const char *qwe, int len) {char *p=asd;assert (ASD); assert (Qwe); while (len) {*asd=*qwe; asd++; qwe++;len--;} *asd= ' + '; return p;} int main () {char Asd[12];char qwe[]= "Zxcrftgytuyji"; char *ret=my_strncpy (asd,qwe,3); Puts (ret); return 0;}
Ideas of strcat and strncat;
Strcat is to implement the connection of two strings, we want to implement the connection of two strings, first define a pointer qwe to the first address of the concatenated string array, when the pointer to the content is not "", the pointer to the address Gaga, when the pointer to the content of " Assigns the contents of the pointer IOP of another array to the content pointed to by the qwe pointer, the Qwe pointer to the address Gaga, and the IOP pointer to the address Gaga. The loop ends when the pointer IOP points to the content "?". Assigns "Qwe" to the content pointed to by the pointer.
#include <stdio.h.h> #include <string.h> #include <assert.h>char *my_strcat (char *qwe,const char *IOP {char *a=qwe;assert (qwe); assert (IOP); while (*qwe!= ') {qwe++;} while (*iop!= ') {*qwe=*iop;qwe++;iop++;} *qwe=*iop;return A;} int main () {char qwe[10]= "ASDFG"; char zxc[]= "IOP";//*zxc= "IOP" char *ret=my_strcat (QWE,ZXC);p UTS (ret); return 0;}
In the same way that the Strncat connection is similar to Strcat, Strncat connects the first n of its array.
#include <stdio.h> #include <string.h> #include <assert.h>char *my_strncat (char *dest,const char *str , int n) {char *p=dest;assert (dest), assert (str), while (*dest!= ') {dest++;} while (n) {*dest=*str;dest++;str++;n--;} *dest= ' + '; return p;} int main () {char qwe[15]= "asdfgh"; char zxc[]= "Qwert"; char *ret=my_strncat (qwe,zxc,3);p UTS (ret); return 0;}
STRCMP is a comparison of the size of two strings;
My idea is to define two string arrays in the main function, char rty[]= "ABCDEFG"; Char cvb[]= "ABCEDFG"; in order to look simple and clear, you can use the calling function, in the main function to pass in the argument, call the function function with a pointer to receive, const char* Rty,const char *CVB, note pointers are best protected with const , so that the pointer is not easily broken, before using the pointer to assert whether the pointer exists, if present, create a loop, enter the loop, when the pointer rty and CVB point to the same content when the loop ends, not equal to enter the loop, in the loop if the pointer rty the content of " Loop ends return, otherwise the pointer rty to the address Gaga that the pointer cvb points to. When two pointers point to something that is not equal to the end of the loop, the return value is subtracted from the content that the two pointers point to, and if the value is greater than 0, RTY>CVB, if the value is less than 0, RTY<CVB, otherwise two strings are equal.
#include <windows.h> #include <string.h> #include <assert.h>char *my_strcmp (const char *rty,const Char *cvb) {assert (Rty), assert (CVB), while (*rty = = *CVB) {if (*rty = = ') ' Return 0;rty++;cvb++;} return (*RTY-*CVB);} int main () {char rty[10]= "ABCDEFG"; char cvb[10]= "ABCEDFG"; int ret=my_strcmp (RTY,CVB); if (ret>0) {printf ("rty large");} else if (ret<0) {printf ("CVB large");} else{printf ("equal");} return 0;}
The above is I share today some of my ideas of writing string code, if you have any suggestions I hope you leave a message, I will improve.
This article is from "Dream" blog, please be sure to keep this source http://12951882.blog.51cto.com/12941882/1976476
C-language simulation implements strcpy functions, strcat functions, strcmp functions