Question

Note that the main function that I have provided does use <string.h> as it constructs test...

Note that the main function that I have provided does use <string.h> as it constructs test strings to pass to your functions. However, your solutions for the 5 functions below may not use any of the built-in C string functions from the <string.h> library.

  1. Write a function called strcmp373. This function is passed two parameters, both of which are C strings. You should use array syntax when writing this function; that is, you may use [ ], but not * or &. The function should return an int as follows:

    1. A negative number if the first string is alphabetically before the second string. In C, the return value of strcmp is a number which reflects the difference in the ASCII codes for the first 2 letters which are not the same. For example, a call to strcmp("programming", "project") returns -3, since 'g' - 'j' in ASCII is -3.

    2. Zero if the strings are the same

    3. A positive number if the second string is alphabetically before the first

      string. Again, the number is the difference in ASCII codes between the first 2 letters which are not the same.

    Here are some examples of how your code should work:

    strcmp373("bin", "bag"); // returns 8
    strcmp373("computer", "game"); // returns -4 strcmp373("computer", "computer"); // returns 0 strcmp373("are", "area"); // returns -97, because '\0'- ‘a’ = 0-97 = - 97.

  2. Write a function called strcat373. The function is passed two parameters, both of which are C strings. You should use pointer syntax when writing this function (in other words, you can use * and &, but not [ ]). The function should modify the first parameter (a char array, often called the destination array) so that it contains the concatenation of the two strings, and returns a pointer to the destination array. For example:

    int main() {
    char str1[9] = "comp";
    char str2[ ] = "uter";
    char *str3;
    strcat373(str1, str2);
    // prints computer twice
    printf("str1 contains %s\nstr3 contains %s\n,

    str1, str3);

Note that strcat373 is guaranteed to work properly only if str1's length is big enough to contain the concatenation of the two strings. In this case, "computer" takes up 9 bytes, and since str1 is 9 bytes, the function should run properly since the array is just long enough to contain “computer” (plus ‘\0’). On the other hand

:

char str1[] = "comp"; // only 5 bytes
char str2[] = "uter";
strcat373(str1, str2); // takes up 9 bytes and

                             // therefore overflows str1

Upon execution, a runtime error may occur, or (even worse) no runtime error will occur, but some other variable(s) in your program may be overwritten.

3. Write a function called strchr373. It is passed 2 parameters: a string and a char Here is the prototype for the function:

char *strchr373(char str[], char ch);
The function should return a pointer to the first instance of ch in str. For

example:

int main {
   char s[ ] = "abcbc";
   printf("%s", strchr373(s, 'b'));
   printf("%s", strchr373(s, 'c');
   printf("%d", strchr373(s, 'd');
// prints bcbc
// prints cbc
// prints 0
  1. Write a function called strncpy373. It is passed 3 parameters: 2 strings and the length of the destination array (the first parameter). The intent of including a third parameter is to prevent overflow of the destination array in case the source array (the second parameter) contains a string that is too long to be stored in the destination. strncpy373 returns a pointer to the destination array.

    For example:

          char s1[] = "comp";
          char s2[] = "systems";
          strncpy373(s1, s2, 4);
          printf("%s\n", s1);
    

    The output of the above code should be syst.

  2. Write a function called strncat373. It performs string concatenation (just like strcat373), except that it limits the number of characters that may be copied from src onto the end of dest.

The main function:

#include <stdio.h>
#include <string.h>

#include "hw2.c"

int main() {
   char word1[7] = "bin",
       word2[] = "bag",
   word3[25] = "computer",
       word4[] = "game",
   word5[] = "area",
       word6[] = "area",
       word7[] = "are",
       word8[7] = "bo",
       word9[] = "ringer",
       word10[7] = "bo";
   int cmp1 = strcmp373(word1, word2),
   cmp2 = strcmp373(word3, word4),
   cmp3 = strcmp373(word5, word6),
   cmp4 = strcmp373(word7, word6);
   printf("Test strcmp373\n");
   printf("%s %s = %d\n", word1, word2, cmp1);
   printf("%s %s = %d\n", word3, word4, cmp2);
   printf("%s %s = %d\n", word5, word6, cmp3);
   printf("%s %s = %s\n", word7, word6, cmp4);
   printf("\nTest strcat373\n");
   printf("%s + %s = ", word1, word2);
   printf("%s\n", strcat373(word1, word2));
   printf("%s + \" \" + %s = ", word3, word4);
   strcat373(word3, " ");
   strcat373(word3, word4);
   printf("%s\n", word3);
   printf("%s + %s = ", word8, word7+1);
   printf("%s\n", strcat373(word8, word7+1));
   printf("\nTest strchr373\n");
   char s[ ] = "abcbc";
   printf("strchr373(%s,%c) = ", s, 'b');
   printf("%s", strchr373(s, 'b')); // prints bcbc
   printf("\n");
   printf("strchr373(%s,%c) = ", s, 'c');
   printf("%s\n", strchr373(s, 'c')); // prints cbc
   printf("\n");
   printf("strchr373(%s,%c) = ", s, 'd');
   printf("%d", strchr373(s, 'd')); // prints 0
   printf("\n");
   printf("\nTest strncpy373\n");
   char s1[] = "comp";
   char s2[] = "systems";
   strncpy373(s1, s2, 4);
   printf("%s\n", s1);
   printf("\nTest strncat373\n");
   printf("%s + %s %d = ", word10, word9, 4 );
   strncat373(word10, word9, 4);
   printf("%s\n", word10);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following implementations will be of immense assistance for your question:

int strcmp373(char word1[],char word2[])

{

int count=0;

while(1 && (word1[count]!='\0' || word2[count]!='\0'))

{

if(word1[count]!=word2[count])

return (int)(word1[count]-word2[count]);

count++;

}

return 0;

}

char* strcat373(char* word1,char* word2)

{

char *index=word1;

while(1)

{

if(*index=='\0')

{

char* index2=word2;

while(1)

{

*index=*index2;

if(*index2=='\0')

break;

index++;

index2++;

}

return word1;

}

index++;

}

}

char* strchr373(char string[],char word)

{

char* index = &string[0];

while(1 && *index!='\0')

{

if(*index==word)

return index;

index++;

}

return NULL;

}

Add a comment
Know the answer?
Add Answer to:
Note that the main function that I have provided does use <string.h> as it constructs test...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CSC Hw Problems. Any help is appreciated I dont know where to start let alone what...

    CSC Hw Problems. Any help is appreciated I dont know where to start let alone what the answers are. Your assignment is to write your own version of some of the functions in the built-in <string.h> C library. As you write these functions, keep in mind that a string in C is represented as a char array, with the '\0' character at the end of the string. Therefore, when a string is passed as a parameter, the length of the...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • using c, Write a function called p4. The function takes 2 parameters, both of which are...

    using c, Write a function called p4. The function takes 2 parameters, both of which are C strings. p4 should return “true” (not 0) if the 2ndparameter is a substring of the 1st parameter, or “false” (0) otherwise. Forexample: char topic[] = “computer”; char substr[] = “put”; printf(“%d\n”, p4(topic, substr); // prints 1 substr[0] = ‘P’; printf(“%d\n”, p4(topic, substr); // prints 0 – case sensitive char s1[] = “ababcd”; char s2[] = “abcd” char s3[] = “baba”; char s4[] =...

  • Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(...

    Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(char *s) {    /* this is here so code compiles */ return 0; } /* array version */ /* concantenate t to the end of s; s must be big enough */ void str_cat(char s[], char t[]) {    int i, j;    i = j = 0;    while (s[i] != '\0')    /* find end of s */        i++;    while ((s[i++] = t[j++]) != '\0') /* copy t */        ;...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and...

    Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop. Remember...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT