Question

Write a C program RemoveWord.c that can remove a word from a given string (i.e. a...

Write a C program RemoveWord.c that can remove a word from a given string (i.e. a line) entered by the user

Sample output is as following:
Please enter a string: This is the last homework.
Please enter the word to be removed: is
Result: This the last homework.

Hint: The C library function gets reads a line from stdin and stores it into the string pointed to by str. char *gets(char *str)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have written the c code based on the requirement given in the question

Please read the inline comment given inside the code, this explains the code functionality

All the comments are hightlighted in bolder text

************************C Code***********************

#include <stdio.h>

#include <string.h>

// Declared maximum string size

#define MAX_SIZE 100

// Function declaration

void RemoveWord(char * userInput, char * wordToRemove);

int main()

{

char userInput[MAX_SIZE];

char wordToRemove[MAX_SIZE];

//Input string and word from user

printf("Please enter a string: ");

gets(userInput);

printf("Please enter the word to be removed: ");

gets(wordToRemove);

  

//Call the remove word method to remove the wordToRemove

//that user entered

RemoveWord(userInput, wordToRemove);

printf("\nResult: %s", userInput);

return 0;

}

//This method removes the occurrences of a given word in string.

void RemoveWord(char * userInput, char * wordToRemove)

{

int i, j, inputWordLength, removeWordLength;

int found;

// Length of the user input string

inputWordLength = strlen(userInput);   

// Length of word that need to remove

removeWordLength = strlen(wordToRemove);

//For loop to match the word to remove from user input string

for(i=0; i <= inputWordLength - removeWordLength; i++)

{

//Match word with string

found = 1;

for(j=0; j<removeWordLength; j++)

{

if(userInput[i + j] != wordToRemove[j])

{

found = 0;

break;

}

}

//If the word is not found

if(userInput[i + j] != ' ' && userInput[i + j] != '\t'

&& userInput[i + j] != '\n' && userInput[i + j] != '\0')

{

found = 0;

}

//If word is found then shift all characters to left

//and decrement the string length

if(found == 1)

{

for(j=i; j<=inputWordLength - removeWordLength; j++)

{

userInput[j] = userInput[j + removeWordLength];

}

inputWordLength = inputWordLength - removeWordLength;

//Match the next occurrence of word from the current index.

i--;

}

}

}

*****************END*******************

Output Screen

Please enter a string: This is the last homework Please enter the word to be removed: the Result: This is last homework Progr

Add a comment
Know the answer?
Add Answer to:
Write a C program RemoveWord.c that can remove a word from a given string (i.e. a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Write a program that can remove spaces from an input string, find the indexes of a...

    Write a program that can remove spaces from an input string, find the indexes of a character within the string and replace that character with another character. Here is an example input: I am an input string a b The first line, "I am an input string" represents the input string. Please put it into a string variable using getline. In the second line "a b", a is the character that needs to be located within the input string, and...

  • Write a program in C that reads a string of bits( so either a one or...

    Write a program in C that reads a string of bits( so either a one or zero) in from the user one char at a time using the function getChar, which returns a char. hint in order to convert a char to an int, subtract the character. Then store the bits into an array. start with this: #include "stdio.h" #define MAX_BITS 32 int main() { printf("Enter up to 32 bits (hit 'enter' to terminate early): "); char bit = getchar();...

  • write a c++ program: User enter in 5 numbers, stores  them in an array then displays them...

    write a c++ program: User enter in 5 numbers, stores  them in an array then displays them from least -> greatest. Then create a char array the user will enter some letters, it gives a count on every 'a' then it gets replaced by an uppercase 'A'. Display the result. Then display only the last letter.

  • The following code is a C Program that is written for encrypting and decrypting a string....

    The following code is a C Program that is written for encrypting and decrypting a string. provide a full explanation of the working flow of the program. #include <stdio.h> int main() { int i, x; char str[100]; printf("\n Please enter a valid string: \t"); gets (str); printf ("\n Please choose one of the following options: \n"); printf ("1 = Encrypt the given string. \n"); printf("2 = Decrypt the entered string. \n"); scanf("%d",&x); // using switch case statements switch (x) {...

  • C++ program, please follow the instruction and do not add any new comment or remove any....

    C++ program, please follow the instruction and do not add any new comment or remove any. /** CIS 22B: Homework 4A Using c-string manipulation functions: strcpy, strcat, strrchr, etc. Write a function that given a c-string of words removes the last word and inserts it in the beginning of the string. All words are separated by spaces. You may assume that there is only one space between two words. Strings that are either empty or consists of only one word...

  • C++ program, please follow the instruction and do not add or remove and comments. Please highlight...

    C++ program, please follow the instruction and do not add or remove and comments. Please highlight any change made to the original code. /** CIS 22B: Homework 4A Using c-string manipulation functions: strcpy, strcat, strrchr, etc. Write a function that given a c-string of words removes the last word and inserts it in the beginning of the string. All words are separated by spaces. You may assume that there is only one space between two words. Strings that are either...

  • The program needs to be written in C. Write a function void camelCase(char* word) where word...

    The program needs to be written in C. Write a function void camelCase(char* word) where word consists of more than two words separated by underscore such as “random_word” or "hello_world_my_name_is_sam". camelCase() should remove underscores from the sentence and rewrite in lower camel case” (https:// en.wikipedia.org/wiki/Camel_case). Watch out for the end of the string, which is denoted by ‘\0’. You have to ensure that legal strings are given to the camelCase() function. The program should only run when the input is...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT