Question: For this program you will implement the following utility functions to test mastery of C strings. *******you MUST use these these function***** void removeBlanks(char *src,
For this program you will implement the following utility functions to test mastery of C strings.
*******you MUST use these these function*****
void removeBlanks(char *src, char *dest);
void replaceChar(char *src, char oldChar, char newChar);
char *flipCase(const char *src);
Please read the description of these functions carefully.
In the removeBlanks function you will implement a routine that takes a string in as src and outputs the same string into dest but removing any blank space character encountered. For example, if the src is Hel lo Wor ld!, this function should return the value HelloWorld! via the dest pointer . 1
In the replaceChar function your function should operate much like a find and replace operation in a word processor works, meaning that the function will replace any instance of the character oldChar and replace it with the character newChar. For example, for a src string Hel lo Wor ld, if oldChar is o and newChar is e, then src is modified as Hel le Were ld!.
In the flipCase function the function turns each lowercase character into an uppercase character and each uppercase character into a lowercase character. For example, if the src string is GNU Image Processing Tool-Kit, then this function should return a string gnu iMAGE pROCESSING tOOL-kIT.
steps for flipCase function: 1. dynamically create a new string (character array) equivalent to the length of src. In this string, you will store the src after the case converstion. 2. Use a FOR LOOP interate through each character of src. Check for upper case and lower case, else assign the current character of src to the new string (no conversion is required) 3. Finally add a null terminating character at the end of the new string and return it.
In your main function, you should prompt the user for a string to use for the first two functions, and a string to use for the third function. The user should be allowed to enter any string including ones with white spaces as input. You should also prompt the user for a character to replace and a replacement character for the flip case function. You may assume that any input string will be at maximum 100 characters.
****************************
So I can't figure out how to fix my code. It is IMPORTANT that it is fixed following the guidlines above.
#include
void removeBlanks(char *src, char *dest); void replaceChar(char *src, char oldChar, char newChar); char *flipCase(const char *src);
int main(void) { char string1[100]; char string2[100]; char before; char after; char destination; printf("Enter string for remove blanks and replace char function functions: "); fgets(string1, 100, stdin); //printf("%s", string1); printf("Enter string for flip case function: "); fgets(string2, 100, stdin); //printf("%s", string2); printf("Enter character to replace: "); scanf("%c", &before); printf("Enter replacement character: "); scanf("%c", &after); printf("1. Remove Blanks "); printf("Remove all blanks in %s", string1); removeBlanks(string1, &destination); printf("%c ", destination); printf("2. Replace Char "); printf("Remove all '%c's in '%s' with '%c's:", before, string1, after); replaceChar(string1, before, after); printf("%s ", string1); printf("3. Flip Case "); printf("Original string is: %s", string2); printf("Flipped case string is: %s ", flipCase(string2)); return 0; } void removeBlanks(char *src, char *dest) { int length = strlen(src); int i = 0; int j = 0; for(i = 0; i < length; i++) { if(src[i] != ' ') { dest[j] = src[i]; j++; } } dest[j] = '\0'; }
void replaceChar(char *src, char oldChar, char newChar) { int length = strlen(src); int i; for(i = 0; i < length; i++) { if(src[i] == oldChar) { //Compare src[i] = newChar; //Assign } } }
char *flipCase(const char *src) { char *tempStr = malloc(strlen(src) + 1); //Copy source to tempStr strncpy(tempStr, src, strlen(src)); int i; //Check if case if lower or upper for(i = 0; i < strlen(src); i++) { if(isupper(src[i])) { tempStr[i] = tolower(src[i]); } else if(islower(src[i])) { tempStr[i] = toupper(src[i]); } else { tempStr[i] = src[i]; } tempStr[i] = '\0'; } return tempStr; //return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
