In: Computer Science
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<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
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:\n");
fgets(string1, 100, stdin);
//printf("%s", string1);
printf("Enter string for flip case
function:\n");
fgets(string2, 100, stdin);
//printf("%s", string2);
printf("Enter character to replace:\n");
scanf("%c", &before);
printf("Enter replacement character:\n");
scanf("%c", &after);
printf("1. Remove Blanks\n");
printf("Remove all blanks in %s", string1);
removeBlanks(string1, &destination);
printf("%c\n", destination);
printf("2. Replace Char\n");
printf("Remove all '%c's in '%s' with '%c's:", before,
string1, after);
replaceChar(string1, before, after);
printf("%s\n", string1);
printf("3. Flip Case\n");
printf("Original string is: %s", string2);
printf("Flipped case string is: %s\n",
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;
}
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
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 string3[100];
char destination[100];
char* afterFlip;
char oldChar;
char newChar;
printf("Enter string for remove blanks and
replace char function functions:");
fgets(string1, 100, stdin);
removeBlanks(string1, destination);
printf("String after removing blanks is: %s \n",
destination);
printf("Enter string for flip case
function:");
fgets(string2, 100, stdin);
afterFlip = flipCase(string2);
printf("Stirng after flip case %s\n",
afterFlip);
printf("Enter string for Replacement:");
fgets(string3, 100, stdin);
printf("Enter replacement character:");
char temp[5];
scanf("%s",temp);
oldChar = temp[0];
printf("Enter new character to replace:");
scanf("%s",temp);
newChar = temp[0];
replaceChar(string3,oldChar,newChar);
printf("Stirng after Replacement %s\n",
string3);
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 = (char *) malloc(sizeof(char) *
(strlen(src)+1));
//Copy source to tempStr
strcpy(tempStr, src);
int i;
//Check if case if lower or upper
for(i = 0; i < strlen(src); i++) {
if(isupper(tempStr[i]))
{
tempStr[i] = tolower(tempStr[i]);
} else
if(islower(tempStr[i])) {
tempStr[i] = toupper(tempStr[i]);
} else {
tempStr[i] = tempStr[i];
}
}
tempStr[i] = '\0';
return tempStr;
}