Back to DFS's C Page


OAC Problem Set III

String Functions
Due 16 Dec 98

  1. Check the man pages for strcpy(), strcat(), strcmp() and strlen() to determine what they do. You may also consult the Using Strings page.
  2. Write your own versions of strcpy(), strncpy(), strcat(), strncat(), strcmp(), strncmp() and strlen(), appending _my to the end of each name.
  3. Use the following code as the main() of your test program.
    int main()
    {
            int cmp; /* return value for concatenation functions */
            char str1[80] = "";
            char str2[80] = "";
    
            cmp = strcmp_my(str1, str2);
            printf("strcmp_my (%d) shows that '%s' and '%s' are the same.\n",
               cmp, str1, str2);
            cmp = strncmp_my(str1, str2, 3);
            printf("strncmp_my (%d) shows that '%.3s' and '%.3s' are the same.\n",
               cmp, str1, str2);
            strcpy_my(str2, "string");
            printf("The length of str2, '%s', is %d.\n",
               str2, strlen_my(str2));
            strncpy_my(str1, &str2[3], 3);
            str1[3] = '\0';
            printf("str1 now contains '%s'.\n", str1);
            strcat_my(str2, str1);
            printf("str2 now contains '%s'.\n", str2);
            strcpy_my(str1, "sing");
            printf("str1 now contains '%s'.\n", str1);
            strncat_my(str1, &str2[3], 3);
            printf("str1 now contains '%s'.\n", str1);
            strcpy_my(str1, "string");
            strcpy_my(str2, "string");
            cmp = strcmp(str1, str2);
            printf("strcmp_my (%d) shows that '%s' and '%s' are the same.\n",
                    cmp, str1, str2);
            strcpy_my(str2, "strong");
            cmp = strcmp_my(str1, str2);
            printf("strcmp_my (%d) shows that '%s' is less than '%s'.\n",
                    cmp, str1, str2);
            cmp = strcmp_my(str2, str1);
            printf("strcmp_my (%d) shows that '%s' is greater than '%s'.\n",
                    cmp, str2, str1);
            cmp = strncmp_my(str1, str2, 3);
            printf("strncmp_my (%d) shows that '%.3s' and '%.3s' are the same.\n",
                    cmp, str1, str2);
            cmp = strncmp_my(str1, str2, 4);
            printf("strncmp_my (%d) shows that '%.4s' is less than '%.4s'.\n",
                    cmp, str1, str2);
            cmp = strncmp_my(str2, str1, 4);
            printf("strncmp_my (%d) shows that '%.4s' is greater than '%.4s'.\n",
                    cmp, str2, str1);
            return 0;
    }
    
  4. The code for the first two lines of output does not need to be done first.
  5. The longest function in my program is 14 lines long.
  6. You should probably start by programming strlen_my() -- it is useful in strncpy_my() and strncmp_my().
  7. Concise, appropriate documentation is required.
  8. Appropriate variable names are a must.
  9. Your output should end up looking like the following:
    strcmp_my (0) shows that '' and '' are the same.
    strncmp_my (0) shows that '' and '' are the same.
    The length of str2, 'string', is 6.
    str1 now contains 'ing'.
    str2 now contains 'stringing'.
    str1 now contains 'sing'.
    str1 now contains 'singing'.
    strcmp_my (0) shows that 'string' and 'string' are the same.
    strcmp_my (-1) shows that 'string' is less than 'strong'.
    strcmp_my (1) shows that 'strong' is greater than 'string'.
    strncmp_my (0) shows that 'str' and 'str' are the same.
    strncmp_my (-1) shows that 'stri' is less than 'stro'.
    strncmp_my (1) shows that 'stro' is greater than 'stri'.
    


Created 9 Dec 98
© DFStermole 1998