{"id":7279,"date":"2024-04-09T16:01:01","date_gmt":"2024-04-09T08:01:01","guid":{"rendered":""},"modified":"2024-04-09T16:01:01","modified_gmt":"2024-04-09T08:01:01","slug":"mystr\u51fd\u6570 mystrcpy mystrncmp mystrtok mystrlen","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7279.html","title":{"rendered":"mystr\u51fd\u6570 mystrcpy mystrncmp mystrtok mystrlen"},"content":{"rendered":"
\/\/mystrcpy\n\n#include <stdio.h>\n\nchar *mystrcpy(char *dest, char *src)\n{\n\tint i;\n\tfor(i = 0; (dest[i] = src[i]); i++){ \n\t\t\t;\t\/\/\u5f53src[i] = \"\\0\" \u8868\u8fbe\u5f0f\u4e3a\uff10\uff0c\uff10\u4e3a\u5047\uff0c\u6761\u4ef6\u4e0d\u6210\u7acb\uff0c\u8df3\u51fa\u5faa\u73af\n\t\t\n\t}\n\n\treturn dest;\n\n}\n\nint main(int argc, char *argv[])\n{\n\tchar dest[200];\n\t\n\tprintf(\"mystrcpy:%s\\n\", mystrcpy(dest, argv[1]));\n\n\treturn 0;\n}\n\n\/*\nakaedu@akaedu-G41MT-D3:~\/lin\/722_str$ .\/mystrcpy hellook\nmystrcpy:hellook\n*\/<pre name=\"code\" class=\"cpp\">\n\n\n\n\/\/*******************************************\n\/\/--------------------mystrcpy1---------------\n\n\n#include <stdio.h>\n#include <string.h>\n\nchar *mystrcpy(char *dest, char *src)\n{\n\tint i = 0;\n\n\tfor(; (dest[i] = src[i]); i++){\n\t\t\t;\n\t}\n\treturn dest;\n\n\n}\n\n\nint main(void)\n{\n\tchar dest[100];\n\tchar src[100];\n\tgets(src);\n\/\/\tfgets(src, 100, stdin);\n\n\tprintf(\"%s\\n\", mystrcpy(dest, src) );\n\n\treturn 0;\n}\n\n\n\n\/*\u5982\u679c\u7528fgets()\u4f1a\u628a\u56de\u8f66\u4e5f\u8bfb\u8fdb\u53bb\/\/\u53bb\u56de\u8f66\u8f93\u51fa\n\tint i = strlen( mystrcpy(dest, src) );\n\tprintf(\"%d\\n\", i);\n\n\tif(dest[i-1] == '\\n')\n\t{\n\t\tdest[i-1] = '\\0';\n\t}\n\n\tprintf(\"%s\\n\", dest);\n\n\treturn 0;\n\t\n}\n--------------------------------------\nakaedu@akaedu-G41MT-D3:~\/lin\/722_str$ .\/mystrcpy1\nhellow\n7\nhellow\n--------------------------------------\n*\/<pre name=\"code\" class=\"cpp\">\n\n\n\n\n\n\n\n\/\/mystrncpy\n\n#include <stdio.h>\nchar *mystrncpy(char *dest, char *src, int n )\n{\n\tint i;\n\n\tfor(i = 0; ( (n > 0) && (dest[i] = src[i]) ); i++, n-- ){\n\t\t\n\t}\n\n\twhile(n >= 0){\n\t\tdest[i++] = '\\0';\n\t\tn--;\n\t}\n\t\n\t\n\t\/\/dest[i++] = '\\0';\n\t\t\n\t\n\treturn dest;\n\n}\n\n\n\n\nint main(int argc, char *argv[])\n{\n\tchar str[200];\n\tprintf(\"mystrncpy:%s\\n\", mystrncpy(str, argv[1], 5));\n\treturn 0;\n}\n\n\/*\nakaedu@akaedu-G41MT-D3:~\/lin\/722_str$ .\/mystrncpy helloaaa\nmystrncpy:hello\nakaedu@akaedu-G41MT-D3:~\/lin\/722_str$ .\/mystrncpy comeonbaby\nmystrncpy:comeo\n*\/<\/code><\/pre>\n <\/p>\n
<\/pre>\n\n\/\/*******************************************\n\/\/--------------------mystrncpy1---------------\n\n\n#include <stdio.h>\n#include <string.h>\n\nchar *mystrncpy(char *dest, char *src, int n)\n{\n\tint i;\n\n\/\/\tfor( i = 0; ( (i > n) &&(dest[i] = src[i]) ); i++){\n\/\/\t\t\t;\n\/\/\t}\n\tfor( i = 0; ( (n > 0) &&(dest[i] = src[i]) ); i++, n--){\n\t\t\t;\t\t\t\n\t}\n\n\twhile( n >= 0){\n\t\tdest[i++] = '\\0';\n\t\tn--;\n\t\n\t}\n\treturn dest;\n\n\n}\n\n\nint main(void)\n{\n\tchar dest[100];\n\tchar src[100];\n\tgets(src);\n\n\/\/\tfgets(src, 100, stdin);\n\n\tprintf(\"%s\\n\", mystrncpy(dest, src, 5) );\n\n\treturn 0;\n}\n\n\n\/*\nakaedu@akaedu-G41MT-D3:~\/lin\/722_str$ .\/mystrncpy1\nhellowor ds\nhello\n*\/\n\n\n<pre name=\"code\" class=\"cpp\">\n\n\n\n\n\/***************************************************\n\u81ea\u5df1\u5b9e\u73b0\u5e93\u51fd\u6570\uff1astrtok\n*\/\n\n#include <stdio.h>\n#include <string.h>\n\nchar *mystrtok(char *src, char *delim)\n{\n\tchar *begin, *end;\n\tstatic char *savep = \"\";\n\n\tbegin = src ? src : savep;\n\tbegin = begin + strspn(begin, delim);\n\tif(*begin == '\\0'){\n\t\tsavep = \"\";\n\t\treturn NULL;\n\t}\n\n\tend = begin + strcspn(begin, delim);\n\tif(*end != '\\0'){\n\t\t*end++ = '\\0';\n\t}\n\tsavep = end;\n\treturn begin;\n}\n\n\nint main(int argc, char *argv[])\n{\n\tchar *token, *begin;\n\n\tfor(begin = argv[1]; (token = mystrtok(begin, argv[2])); begin = NULL ){\n\t\n\t\tprintf(\"token:%s\\n\", token);\n\t}\n\n\n\n\n\treturn 0;\n}<\/code><\/pre>\n <\/p>\n
<\/pre>\n\n\n\n\n\n\n\n\/\/************************************\n\/\/-----------mystrlen-----------------\n\n#include <stdio.h>\n\nint mystrlen(char *src)\n{\n\tint i = 0;\n\tfor(i = 0; (src[i] != '\\0') ; i++){\n\n\t\tprintf(\"src[%d]:%d %c\\n\", i, i, src[i]);\n\n\t}\n\n\treturn i;\n\n}\n\n\nint main(void)\n{\n\tchar str[1024];\n\tfgets(str, 1024, stdin);\n\/\/\tgets(str);\n\tprintf(\"%d\\n\", mystrlen(str));\n\n\treturn 0;\n}\n\n\/*----------------------\nhello\nsrc[0]:0 h\nsrc[1]:1 e\nsrc[2]:2 l\nsrc[3]:3 l\nsrc[4]:4 o\nsrc[5]:5 \n\n5\n-----------------------*\/\r\n\r\n<pre id=\"best-content-1042162848\" class=\"best-text mb-10\">fgets\u7684\u539f\u578b\u662f\nchar* fgets(char* s, int n, FILE* fp);\n\u53c2\u6570\u6570\u91cf\u6bd4\u8f83\u591a\uff0c\u67093\u4e2a\u3002\u800cfgets\u76f8\u6bd4\u4e8egets\u6709\u4e00\u4e2a\u663e\u8457\u7684\u5dee\u522b\u5c31\u662ffgets\u4f1a\u5c06\u884c\u672b\u7684\u6362\u884c\u7b26\u7b97\u5230\u8bfb\u5165\u7684\u5b57\u7b26\u4e32\u91cc\u9762\u3002\u6240\u4ee5\u76f8\u540c\u4e14\u6b63\u5e38\uff08\u8f93\u5165\u65e0\u9519\u8bef\uff0c\u7f13\u51b2\u533a\u591f\u5927\uff09\u7684\u60c5\u51b5\u4e0b\uff0cfgets\u8bfb\u5165\u7684\u5b57\u7b26\u4e32\u4f1a\u6bd4gets\u5728\u672b\u5c3e'\\0'\u524d\u9762\u591a\u4e00\u4e2a\u6362\u884c\u7b26\uff1b\u884c\u957f\u5ea6\u8d85\u51fa\u7f13\u51b2\u533a\u5927\u5c0f\u65f6\u53ea\u8bfb\u5165\u524d n-1 \u4e2a\u5b57\u7b26\u3002\u56e0\u6b64\uff0c\ngets(s);\n\u76f8\u5f53\u4e8e\nfgets(s, sizeof(s), stdin); \nif(s[strlen(s) - 1] == '\\n') s[strlen(s) - 1] = '\\0'; \/\/ \u53bb\u6389\u6362\u884c\u7b26<\/code><\/pre>\n <\/p>\n
<\/pre>\n <\/p>\n
<\/pre>\n<\/pre>\n<\/pre>\n<\/p>\n","protected":false},"excerpt":{"rendered":"mystr\u51fd\u6570 mystrcpy mystrncmp mystrtok mystrlen\/\/mystrcpy#includechar*mystrcpy(char*dest,char*src){\tinti;\tfor(i=0;(des...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7279"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7279"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7279\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}