Question

In: Computer Science

For this program you will implement the following utility functions to test mastery of C strings....

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;
}

  
  
  
  

Solutions

Expert Solution

#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;
}


Related Solutions

C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any...
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function returns 2. int...
C coding • Implement, using structures and functions as appropriate, a program which requires you to...
C coding • Implement, using structures and functions as appropriate, a program which requires you to enter a number of points in 3 dimensions. The points will have a name (one alphanumeric character) and three coordinates x, y, and z. Find and implement a suitable way to stop the input loop. The program, through an appropriate distance function, should identify the two points which are the furthest apart. Another function should calculate the centre of gravity of the point cloud...
Design and implement a C++ program with functions to calculate the pre-tax charge: If the customer...
Design and implement a C++ program with functions to calculate the pre-tax charge: If the customer subscribes to a phone plan called Plan200, then he is charged $5 for the first 200 minutes. For each additional minutes, the customer will be charged $0.10. If the customer subscribes to a phone plan called Max20, then he is charged $0.05 for each minute up to $20. (I.e., the customer never needs to pay more than $20.) If the customer is not subscribed...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
In this problem you will design and implement C++ code that identifies overlap in strings. Specifically,...
In this problem you will design and implement C++ code that identifies overlap in strings. Specifically, design and implement a C++ program that does the following: 1. Asks a user to input a filename and then opens that file. If the file open fails, then print the message “Unable to open file” and terminate the program using exit(1). 2. Reads the file contents, in order, into an array of strings. (See the file format explanation below.) 3. Computes the string...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT