In: Computer Science
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)
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: