Question

In: Computer Science

Assignment 2 (worth 10% of the final course grade - due date July 23, 2019) Prof....

Assignment 2 (worth 10% of the final course grade - due date July 23, 2019) Prof. Sherif Saad Purpose The purpose of this assignment is to help you to practice working with C String, arrays, and pointers Learning Outcomes ● Develop skills with pointers ● Learn how to traverse C String using pointers ● Passing Strings to functions ● Manipulate C String ● Develop algorithm design skills Problem Overview You will implement a basic string manipulation library called xstring.h. This library provides the ability to manipulate and search native C strings. The library offers the following functions int FindFirstOccurrence(char c , char *pStr ); //[5 Points] /* This function takes two parameters the first is a variable of type char and the second is a pointer of type char that point to a string variable. The function will parse the string and return the index of the first occurrence of the char variable in the string. If the character does not exist, it will return -1 example pStr = "Hello World!" c = 'o' The function will return 4, because the first o occurred at index 4 in the string */ int FindLastOccurrence(char c, char * ); //[5 Points] /* This function takes two parameters the first is a variable of type char and the second is a pointer of type char that point to a string variable. The function will parse the string and return the index of the last occurrence of the char variable in the string. If the character does not exist, it will return -1 University of Windsor COMP141- Summer 2019 School of Computer Science example pStr = "Hello World!" c = 'o' The function will return 7, because the last o occurred at index 4 in the string */ int GetStringWeight (char *pStr ); // [10 Points] /* This function takes one parameter, which is a char pointer variable that points to a C string. The function will calculate and return the weight of the C string. The weight of the C string is the total sum of the ASCII code representation of the characters in that string. example pStr = "Cat" The function will return 280 since ASCII('C') = 67, ASCII('a') = 97 , and ASCII('t') = 116 */ void ReplaceCharacter(char x , char c , char * pStr); // [10 points] /* This function takes three parameters the first and the second are variables of type char, and the third is a pointer of type char that point to a string variable. The function will parse the string using pStr and every time it finds a character that matches the value of x it will replace it with the value stored in c. example pStr = "banana and apple" x = 'a' c = 'e' The function will change pStr to "benene end epple" */ University of Windsor COMP141- Summer 2019 School of Computer Science void ToUpperCase(char * pStr); // [10 points] /* This function takes one parameter, which is a char pointer variable that points to a C string. The function will convert all the lower case alphabetic characters to uppercase characters. example pStr = "The Cat in The Hat" The function will change pStr to "THE CAT IN THE HAT" */ void ToLowerCase(char *pstr); // [10 points] /* This function takes one parameter, which is a char pointer variable that points to a C string. The function will convert all the uppercase alphabetic characters to lowercase characters. example pStr = "COMPUTER" The function will change pStr to "computer" */ void RemoveCharacter(char c , char str[] ); // [15 points] /* This function takes two parameters the first is a variable of type char and the second is a char array that store to a C string. The function will parse the C string and remove any character in the string that matches the character value stored in c example str = "Cats Hats Tags" c = 's' The function will change str to "Cat Hat Tag" */ University of Windsor COMP141- Summer 2019 School of Computer Science void ReverseString(char *pStr); // [15 points] /* This function takes one parameter, which is a char pointer variable that points to a C string. The function will reverse the string. example pStr = "Pass the test" The function will change pStr to "tset eht ssaP" */ Instructions You will use the provide code skeleton to complete the assignment. The code skeleton consist of three files. These three files are main.c, xstring.c and xstring.h. The main.c conatins the main program that allows the user to enter any string and characters to manipulate using the xstring library. The xstring.h file contains the function prototypes of the xstring library. Finally, the xstring.c file contains the implementation (function definition) of the xstring library. You are required to: 1. Complete the implementation of the functions and fix any existing bug in the code functions [80 points] 2. Link the main.c file and the xstring.c file using the xstring.h file as shown the lecture notes, please see lecture 3 [10 points] 3. Compile and run the main to test your implementation [10 points] IMPORTANT NOTES: 1. You can create any additional function you think they are necessary to implement the xstring library. 2. You should NOT use any built-in function from the C String library (e.g strlen, strcpy, etc)

Solutions

Expert Solution

Code:

xstring.h

#ifndef XSTRING_H
#define XSTRING_H

int FindFirstOccurrence(char c , char *pStr );
int FindLastOccurrence(char c, char * );
int GetStringWeight (char *pStr ); 
void ReplaceCharacter(char x , char c , char * pStr);
void ToUpperCase(char * pStr);
void ToLowerCase(char *pstr);
void RemoveCharacter(char c , char str[] );
void ReverseString(char *pStr);
//function to calculate length of string pStr
int lengthOfString(char *pStr);

#endif

xstring.c

#include "xstring.h"

int lengthOfString(char *pStr){
        int i;
        for(i=0; pStr[i]!='\0'; i++ );
        return i;
}
int FindFirstOccurrence(char c , char *pStr ){
        int i;
        for(i=0; pStr[i]!='\0'; i++ ){
                if(pStr[i]==c)
                        return i;
        }
        return -1;
}

int FindLastOccurrence(char c, char * pStr ){
        int i, l=lengthOfString(pStr);
        for(i=l-1; i>=0; i-- ){
                if(pStr[i]==c)
                        return i;
        }
        return -1;
}

int GetStringWeight (char *pStr ){
        int i, weight=0;
        for(i=0; pStr[i]!='\0'; i++ ){
                weight += pStr[i];
        }
        return weight;
}

void ReplaceCharacter(char x , char c , char * pStr){
        int i;
        for(i=0; pStr[i]!='\0'; i++ ){
                if(pStr[i]==x)
                        pStr[i] = c;
        }
}

void ToUpperCase(char * pStr){
        int i;
        for(i=0; pStr[i]!='\0'; i++ ){
                if(pStr[i]>=97 && pStr[i]<=122){
                        pStr[i] = pStr[i]-32;
                }
        }       
}

void ToLowerCase(char *pStr){
        int i;
        for(i=0; pStr[i]!='\0'; i++ ){
                if(pStr[i]>=65 && pStr[i]<=90){
                        pStr[i] = pStr[i]+32;
                }
        }
}

void RemoveCharacter(char c , char str[] ){
        int i, j,n;
        n = lengthOfString(str);
        for (i=j=0; i<n; i++) 
                if (str[i] != c) 
                        str[j++] = str[i]; 
        
        str[j] = '\0'; 
}

void ReverseString(char *pStr){
        int i, j, l;
        char ch;
        l = lengthOfString(pStr);
        for(i=l-1, j=0; i>=j; i--, j++ ){
                ch = pStr[j];
                pStr[j] = pStr[i];
                pStr[i] = ch;
        }
}

main.c

#include <stdio.h>
#include"xstring.h"

void main(void){
        //testing xstring library
        char str[] = "Hello World", str2[]="banana and apple",
                        str3[]="The Cat in The Hat", str4[]="Pass the test";
        char *pStr = "Hello World!";
        printf("FindFirstOccurance('%c', '%s')= %d\n",'o', pStr,FindFirstOccurrence('o', pStr) );
        printf("FindFirstOccurance('%c', '%s')= %d\n", 'x', pStr,FindFirstOccurrence('x', pStr) );
        printf("FindLastOccurance('%c', '%s')= %d\n",'o', pStr,FindLastOccurrence('o', pStr) );
        printf("FindLastOccurance('%c', '%s')= %d\n",'x', pStr,FindLastOccurrence('x', pStr));
        pStr = "Cat";
        printf("GetStringWeight('%s')= %d\n",pStr,GetStringWeight(pStr) );
        printf("ReplaceCharacter('%c', '%c', '%s')= ",'a', 'e', str2);
        ReplaceCharacter('a', 'e', str2);
        printf("%s\n", str2);
        printf("ToUpperCase('%s')= ",str3);
        ToUpperCase(str3);
        printf("%s\n", str3);
        printf("ToLowerCase('%s')= ",str3);
        ToLowerCase(str3);
        printf("%s\n", str3);
        printf("RemoveCharacter('%c', '%s') = ",'o', str);
        RemoveCharacter('o', str);
        printf("%s\n", str);
        printf("ReverseString('%s')= ", str4);
        ReverseString(str4);
        printf("%s\n", str4);
}

Output:


Related Solutions

This assignment is worth 4.0% of your final grade and is due by Thursday, 11:59 PM...
This assignment is worth 4.0% of your final grade and is due by Thursday, 11:59 PM ET of week 7. Instructions There are many tools available to assist you as an RN to organize your thoughts, make nursing judgments, and implement the 5 rights of delegation. SBAR – Situation, Background, Assessment, and Recommendation, is a standardized tool used in many institutions that provides a framework to facilitate clear communication between health care providers. The components of SBAR are as follows,...
This assignment worth 10% of your total course grade. Write spim program and execute it on...
This assignment worth 10% of your total course grade. Write spim program and execute it on mars. Your program reads two integer values x, y. Both x and y must be single digit number > 0. If the user does not enter a value that meets this condition ask the user to enter a new value again. Also Write two functions. The first function call it SumEven that gets x and y passed as parameters and returns the sum of...
Assignment 1 (assessment worth 10%) Due Date Monday 8th May by 5pm GMT+8 [Submission will be...
Assignment 1 (assessment worth 10%) Due Date Monday 8th May by 5pm GMT+8 [Submission will be strictly observed. Make submission via Turnitin] Question 1 An Australian investor holds a one month long forward position on USD. The contract calls for the investor to buy USD 2 million in one month’s time at a delivery price of $1.4510 per USD. The current forward price for delivery in one month is F= $1.5225 per USD. Suppose the current interest rate interest is...
Assignment 2 (assessment worth 15%) Due Date 24 May at 5pm GMT+8 [Submission will be strictly...
Assignment 2 (assessment worth 15%) Due Date 24 May at 5pm GMT+8 [Submission will be strictly observed. Make submission via Turnitin] Question 1 Assume Alpha Ltd is currently trading on the NYSE with a stock price of $65. The American one-year call option on the stock is trading at $20 with strike price of $65. If the one-year rate of interest is 10% p.a. (continuously compounding), is the call price free from arbitrage or is it too cheap/expensive, assuming that...
This is part I worth 25% of your final paper grade!! By Week 2, you will...
This is part I worth 25% of your final paper grade!! By Week 2, you will select a health behavior issue that you would like to improve and set out your long and short term goals. Please do the following by the end of Week 2: 100 points (25% of your total final paper and 5% out of your entire course grade) 1) Identify the behavior you wish to change                   10 points (Is the behavior smoking? Overeating? Being sedentary? Is...
By the due date assigned, you will write the final 2 reports, referring to the departments...
By the due date assigned, you will write the final 2 reports, referring to the departments of Endocrinology and Neurology and use them as your script for your Week 5 Oral Report. Your writing section for this assignment will include 2 paragraphs for each department. In order to earn the maximum credit for the written report you need to incorporate at least 10 medical terms for each department, using them in a manner that demonstrates your knowledge of their meaning....
ASSIGNMENT 1 · ECON 3060 - 01 Due Date: September 4, 2019 1) The Graduate Management...
ASSIGNMENT 1 · ECON 3060 - 01 Due Date: September 4, 2019 1) The Graduate Management Admission Test (GMAT) is a standardized test used by schools to determine the aptitude of individuals who are applying for MBA programs. The range of the GMAT score is 200-800. Brian has recently taken the exam and scored 720. This is an example of _______ data. A) nominal B) ordinal C) interval D) ratio 2) A respondent of a survey indicates that she is...
Module 10 Course Project - Final Submission Prepare a two to three page written assignment that...
Module 10 Course Project - Final Submission Prepare a two to three page written assignment that includes the following: Introduction to the assignment (sections of the assignment: caring, professional identity, and professional organizations/associations) Explain your belief of caring in nursing (self, nursing, environment, and profession) Describe your professional identity including your beliefs, values, motives, and experiences Discuss one to two professional organizations/associations you plan to be involved in during your nursing career Conclusion (reflect on the criteria of the assignment;...
Your participation and report on this project will be worth 20% of your course grade. The...
Your participation and report on this project will be worth 20% of your course grade. The assignment that you select should be a minimum of 1000 words in length. Title page, and bibliography are also needed. Find out the position or teaching held by your home church on the following issues: premarital sexual activity masturbation homosexual orientation homosexual behavior
Complete the following Module 2 Written Homework Assignment for this module by the stated due date...
Complete the following Module 2 Written Homework Assignment for this module by the stated due date on the Schedule. Please submit in the Module 2 Written Homework Assignment folder as a .doc, .docx, .pdf or .rtf file. Instructions Complete the following questions in the form of short essays. Each question is worth 6 points. Be sure to cite your references as needed. Type all responses following each question on this assignment page and submit to the folder. All-you-can-eat restaurants allow...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT