{"action":"created","issue":{"url":"https://api.github.com/repos/wujr5/c-and-cpp-language-learning/issues/33","labels_url":"https://api.github.com/repos/wujr5/c-and-cpp-language-learning/issues/33/labels{/name}","comments_url":"https://api.github.com/repos/wujr5/c-and-cpp-language-learning/issues/33/comments","events_url":"https://api.github.com/repos/wujr5/c-and-cpp-language-learning/issues/33/events","html_url":"https://github.com/wujr5/c-and-cpp-language-learning/issues/33","id":119112715,"number":33,"title":"计科：week10 第六次实验报告","user":{"login":"wujr5","id":7693440,"avatar_url":"https://avatars.githubusercontent.com/u/7693440?v=3","gravatar_id":"","url":"https://api.github.com/users/wujr5","html_url":"https://github.com/wujr5","followers_url":"https://api.github.com/users/wujr5/followers","following_url":"https://api.github.com/users/wujr5/following{/other_user}","gists_url":"https://api.github.com/users/wujr5/gists{/gist_id}","starred_url":"https://api.github.com/users/wujr5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wujr5/subscriptions","organizations_url":"https://api.github.com/users/wujr5/orgs","repos_url":"https://api.github.com/users/wujr5/repos","events_url":"https://api.github.com/users/wujr5/events{/privacy}","received_events_url":"https://api.github.com/users/wujr5/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/wujr5/c-and-cpp-language-learning/labels/%E8%AE%A1%E7%A7%91%E7%8F%AD","name":"计科班","color":"207de5"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-11-27T00:10:23Z","updated_at":"2015-12-05T11:57:04Z","closed_at":null,"body":"# 计科：week10 C String and Functions\r\n\r\n## Description\r\n\r\n> The C programming language has a set of functions implementing operations on strings (character strings and byte strings) in its standard library. Various operations, such as copying, concatenation, tokenization and searching are supported. For character strings, the standard library uses the convention that strings are null-terminated: a string of n characters is represented as an arrays of an n + 1 elements, the last of which is a \"NUL\" character.\r\n\r\n> The only support for strings in the programming language proper is that the compiler translates quoted string constants into null-terminated strings.\r\n\r\n*From wikipedia*\r\n\r\nString is a very important thing when we are programming. All of the input and ouput are strings and any kind of data can be translated into a \"String Form\" include int, double and so on. In other words, an integer or a double number can be represented by a string simply.\r\n\r\nStrings in C programming language is implemented by using an character array whose length is n+1(n represents the valid length of the string). So the operations on Strings in C can be treated as the operations for arrays. The following standard C library includes some basic Cstring operations.\r\n\r\n#### <cstring>\r\n\r\nThis header file defines several functions to manipulate C strings and arrays.\r\n\r\n#### Functions\r\n\r\n##### Copying\r\n\r\n**memcpy**\r\nCopy block of memory (function )\r\n**memmove**\r\nMove block of memory (function )\r\n**strcpy**\r\nCopy string (function )...\r\n**strncpy**\r\nCopy characters from string (function )\r\n\r\n##### Concatenation:\r\n**strcat**\r\nConcatenate strings (function )\r\n**strncat**\r\nAppend characters from string (function )\r\n\r\n##### Comparison:\r\n**memcmp**\r\nCompare two blocks of memory (function )\r\n**strcmp**\r\nCompare two strings (function )\r\n**strcoll**\r\nCompare two strings using locale (function )\r\n**strncmp**\r\nCompare characters of two strings (function )\r\n**strxfrm**\r\nTransform string using locale (function )\r\n\r\n##### Searching:\r\n**memchr**\r\nLocate character in block of memory (function )\r\n**strchr**\r\nLocate first occurrence of character in string (function )\r\n**strcspn**\r\nGet span until character in string (function )\r\n**strpbrk**\r\nLocate characters in string (function )\r\n**strrchr**\r\nLocate last occurrence of character in string (function )\r\n**strspn**\r\nGet span of character set in string (function )\r\n**strstr**\r\nLocate substring (function )\r\n**strtok**\r\nSplit string into tokens (function )\r\n\r\n##### Other:\r\n**memset**\r\nFill block of memory (function )\r\n**strerror**\r\nGet pointer to error message string (function )\r\n**strlen**\r\nGet string length (function )\r\n\r\n##### Macros\r\n**NULL**\r\nNull pointer (macro )\r\n\r\n##### Types\r\n**size_t**\r\nUnsigned integral type (type )\r\n\r\n*From cplusplus.com*\r\n\r\n## Separate source file and header file\r\nFor header file, we just write the declaration of the functions and constants.\r\nFor source file, we implement the functions(function definitions)\r\nFor example:\r\n\r\n**func.h**\r\n\r\n```cpp\r\n#ifndef FUNC_H_INCLUDED\r\n#define FUNC_H_INCLUDED\r\nint plus(int a, int b);\r\n#endif //FUNC_H_INCLUDED\r\n```\r\n\r\nfunc.cpp\r\n```cpp\r\n#include \"func.h\"\r\nint plus(int a, int b) {\r\n    return a+b;\r\n}\r\n```\r\n\r\n## Basic Assignment (90pts):\r\nThis time your task is not to just use the functions simply. For this assignment, you will be given serveral function prototypes and your job is to implement the functions with the descriptions and smaples.\r\n\r\n```cpp\r\n#ifndef STRING_H_INCLUDED\r\n#define STRING_H_INCLUDED\r\n/*\r\n   Notice all the samples are use for function logic, not exactly the grammar\r\n*/\r\n\r\n/*\r\n    function: myStrlen\r\n    @description: Calculate the length of the string\r\n    @input: const string s\r\n    @output: the length of the string\r\n    @sample input: \"abcde\\0abcde\"\r\n    @sample output: 5\r\n    @Notice:  Function myStrlen() show check whether the input array is NULL.\r\n    If so, you show return 0. Note that strlen() in string.h does not check NULL arrays.\r\n*/\r\nunsigned int myStrlen(const char s[]);\r\n\r\n/*\r\n    function: myStrcpy\r\n    @description: copy one string to another\r\n    @input: const string source\r\n    @output: string destination\r\n    @sample input: \"a\" \"abc\"\r\n    @sample output: \"abc\"\r\n    @Notice: You can not assign the array directly which will cause a runtime error.\r\n*/\r\nchar * myStrcpy(char destination[], const char source[]);\r\n\r\n/*\r\n    function: myStrcat\r\n    @input: const string source\r\n    @output: string destination\r\n    @sample input: \"abcde\" \"abc\"\r\n    @sample output: \"abcdeabc\"\r\n    @Notice: You can not assign the array directly which will cause a runtime error.\r\n*/\r\nchar * myStrcat(char destination[], const char source[]);\r\n\r\n/*\r\n    function: myStrcmp\r\n    @input: const string1 and const string2\r\n    @output: if string1 is euqal to string2, output 0\r\n             if string1 is greater than string2 ouput an integer > 0\r\n             if string2 is greater than string1 output an integer < 0\r\n    @sample input: \"abc\" \"abc\"\r\n    @sample output: 0\r\n*/\r\nint myStrcmp(const char str1[], const char str2[]);\r\n\r\n/*\r\n    function: Mystrfind\r\n    @input: cosnt string1 and const string2\r\n    @output: if str2 is a substring of str1, output the first index in str1\r\n             otherwise, ouput -1 which indicates can not find\r\n    @sample input: \"abcde\" \"cde\"\r\n    @sample output: 2\r\n*/\r\nint MyStrfind(const char str1[], const char str2[]);\r\n\r\n/*\r\n    function: LeftRotateString\r\n    @input: string buff, an integer n which indicates the first n\r\n    @output: put the first n chars to the end of the string\r\n    @sample input: \"abcdefg\" 4\r\n    @sample output: \"efgabcd\"\r\n*/\r\nvoid LeftRotateString(char *buff, int n);\r\n\r\n/*\r\n    function: myParseInt\r\n    @input: a const string\r\n    @output: an integer parse from a string\r\n    @sample input: \"123\"\r\n    @sample output: 123\r\n    @Notice: You should notice show extreme conditions such as:\r\n   1 null array input: the input is an array, the program will crash in the access to the null array, \r\n       so you need to judge whether the array is null before using the array.\r\n   2 The sign: integer not only contain numbers, and may is the positive integer expressed\r\n       starts with a '+' or '-', so if the first character is' - ', \r\n       to get the integer value is converted to a negative integer.\r\n   3 illegal characters: the input string may contain characters that are not numbers. \r\n     Therefore, whenever you encounter these illegal characters, the program should stop converting.\r\n   4 integer overflow: the number of input is the string of the form of input,\r\n      so the input of a very long string will likely lead to overflow. You should set the return \r\n      value to MAX_INT or MIN_INT in the situation.\r\n*/\r\nint myParseInt(const char str[]);\r\n\r\n/*\r\n    function: myStrcontain\r\n    @input: const string1 and const string2\r\n    @output: a boolean value. id all chars in str2 are in str1, output true\r\n             else output flase\r\n    @sample input1: \"ABCD\" \"BAD\"\r\n    @sample output1: 1\r\n    @sample input2: \"ABCD\" \"BCE\"\r\n    @sample output2: 0\r\n*/\r\nint myStrcontain(const char str1[], const char str2[]);\r\n\r\n#endif // STRING_H_INCLUDED\r\n```\r\n\r\n### Requirements:\r\n\r\n1. You need to write a file myString.cpp for all the implements of the function prototypes.\r\n2. In file myString.cpp **you can not include <cstring> any other standard C or C++ library, the file should be the same type like the following:**\r\n    ```cpp\r\n    #include \"myString.h\"\r\n    #ifndef NULL\r\n    #define NULL 0\r\n    #endif // NULL\r\n\r\n    /*code*/\r\n    /*function definations*/\r\n    /*code end*/\r\n    ```\r\n\r\n3. **You can not change the function prototypes.**\r\n4. Black box testing will be again applied this experiment. **Please check your file names for three times**.\r\n5. **Please do not add main function in your file**, you can only add it when you are testing. \r\n\r\n## Deep Thinking(10pts)\r\n\r\n1. For the function `char * myStrcat(char destination[], const char source[]);`, the array destination indicates the result of the function, why does the function add a return value `char *`? Why not `void myStrcat(char destination[], const char source[]);`?（Microsoft interview question）\r\n2. Many functions in C standar library <string.h> do not check NULL array and they also do not carry about the size of the array. Why? And why strlen_s in c11 provide size check? (Google interview questions)\r\n3.  We have a small C program:\r\n\r\n    ```cpp\r\n    #include <iostream>\r\n    using namespace std;\r\n\r\n    int main() {\r\n        char s1[100] = \"abcdefg\";\r\n        char * s2 = \"abcdefg\";\r\n        s1[0] = 'z';\r\n        s2[0] = 'z';\r\n\r\n        return 0;\r\n    }\r\n    ```\r\nWhat will happen when we run the program and why? What if we remove `s2[0] = 'z';`?\r\n4. Why separate the source file and the header file? Think about the reasons. (Microsoft interview question)\r\n5. How can we improve `LeftRotateString` or `myStrcontain` by other algorithm? (optional question, Microsoft interview questions)\r\nHint: String Reverse can be applied for LeftRotateString and hash table can be used for myStrcontain.\r\n\r\n## Report:\r\nJust answer the questions in deep thinking part\r\n\r\n## Submit (Please pay attention to this part)\r\n\r\n~~作业提交方式：作业由学委收好，再发到我邮箱.~~\r\n\r\n### 提交方式：FTP\r\n地址: ftp://172.18.182.75/\r\n远程目录: Experiment/计科班/Experiment6\r\n**命名规范：13331314_叶嘉祺_EX6_v0.zip**\r\n**注意v0代表你提交的版本号，第一次提交为v0，第二次为v1，以此类推**\r\n**注意，要打包压缩程ZIP格式**\r\n\r\n```cpp\r\n--13331314_叶嘉祺_EX6.zip\r\n  |--myString.cpp\r\n  |--report.pdf\r\n```\r\n\r\n**注意，文件压缩包里不能出现文件夹，只能包含一层目录**\r\n\r\n作业缓交或补交：每缓交一天 -5% 总分\r\n\r\n## Deadline\r\n\r\n12月10日 18:00\r\n\r\n## 最后\r\n\r\n1、文件命名绝对不能错\r\n2、你需要先学习如何写一个函数库。也就是第一个任务，如何编写.h文件和.cpp文件分开，然后在main函数中调用.h文件中的函数。说白了就是明白#include <iostream>这种实现的机制。\r\n3、.h文件和.cpp文件请严格遵守要求中的约定。设定好的函数原型和.h文件请不要更改，并且理解为什么一字不能改？对于系统构架师（编写.h的工程师）和具体实现的工程师（编写.cpp文件工程师），以及测试工程师（编写main.cpp进行测试的工程师），和用户（调用库函数的人）之间是怎样合作，怎样沟通理解的？\r\n4、请先自己尝试编写main函数进行测试（文件分离，再编写）\r\n"},"comment":{"url":"https://api.github.com/repos/wujr5/c-and-cpp-language-learning/issues/comments/162175937","html_url":"https://github.com/wujr5/c-and-cpp-language-learning/issues/33#issuecomment-162175937","issue_url":"https://api.github.com/repos/wujr5/c-and-cpp-language-learning/issues/33","id":162175937,"user":{"login":"SgLy","id":7745065,"avatar_url":"https://avatars.githubusercontent.com/u/7745065?v=3","gravatar_id":"","url":"https://api.github.com/users/SgLy","html_url":"https://github.com/SgLy","followers_url":"https://api.github.com/users/SgLy/followers","following_url":"https://api.github.com/users/SgLy/following{/other_user}","gists_url":"https://api.github.com/users/SgLy/gists{/gist_id}","starred_url":"https://api.github.com/users/SgLy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SgLy/subscriptions","organizations_url":"https://api.github.com/users/SgLy/orgs","repos_url":"https://api.github.com/users/SgLy/repos","events_url":"https://api.github.com/users/SgLy/events{/privacy}","received_events_url":"https://api.github.com/users/SgLy/received_events","type":"User","site_admin":false},"created_at":"2015-12-05T11:57:04Z","updated_at":"2015-12-05T11:57:04Z","body":"@wujr5 myStrfind有效率要求吗……要不要写KMP"}}