[Solved] Remove the occurrence with C++

Remove “the” occurrence: After the weekend holidays, schools opens today. The class teacher found many students were absent. So very next day when students reach class, she announced to submit leave letter. Shrawanti has the habit of including the word “the” frequently in all her sentences. The teachers found it and asked her to remove the occurrences of word “the” from the letter. Can you please help her out?

Write a program to remove the occurrence of “the” word from entered string.

Remove the occurrence with C++

Input format:

Get a sentence from the user.

Output Format:

Remove “the” occurence from the input string. Refer the Sample input and output.
[All text in bold corresponds to input and the rest corresponds to output.]


Sample Input and Output 1:
Enter the string
remove the occurrence of the word from entered string
Result string is
remove occurrence of word from entered string
 
[Hint for C programming: To get a whole sentence use the following statement:
scanf(“%[^\n]”,a);
]

Solution

#include <bits/stdc++.h>
using namespace std;
#define MAX_LIMIT 1000

void removeOccurrenceOfTheWord(char **input_string)
{
    char new_string[MAX_LIMIT];
    const char *the_string = "the";
    // sauravhathi
    int new_string_idx = 0;
    for (int i = 0; i < strlen(*input_string); ++i)
    {
        char current_string[4];
        for (int j = i; j < strlen(*input_string) && j < i + 3; j += 1)
        {
            current_string[j - i] = tolower((unsigned char)(*input_string)[j]);
        }
        current_string[3] = '\0';
        // sauravhathi
        if (strcmp(current_string, the_string) == 0)
        {

            if ((i == 0 && ((*input_string)[i + 3] == '\0' || (*input_string)[i + 3] == ' ')) || (i > 0 && (*input_string)[i - 1] == ' ' && ((*input_string)[i + 3] == '\0' || (*input_string)[i + 3] == ' ')))
            {

                i += 3;
                continue;
            }
        }
        new_string[new_string_idx] = (*input_string)[i];
        new_string_idx += 1;
    }

    new_string[new_string_idx] = '\0';
    *input_string = new_string;
}
int main()
{
    char *input_string = (char *)malloc(MAX_LIMIT * sizeof(char));
    printf("Enter the string\n");
    scanf("%[^\n]%*c", input_string);
    removeOccurrenceOfTheWord(&input_string);
    // sauravhathi
    printf("Result string is\n");
    printf("%s", input_string);
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_LIMIT 1000

void removeOccurrenceOfTheWord(char **input_string)
{
    char new_string[MAX_LIMIT];
    const char *the_string = "the";
    int new_string_idx = 0;
    // sauravhathi
    for (int i = 0; i < strlen(*input_string); ++i)
    {
        char current_string[4];
        for (int j = i; j < strlen(*input_string) && j < i + 3; j += 1)
        {
            current_string[j - i] = tolower((unsigned char)(*input_string)[j]);
        }
        current_string[3] = '\0';
        // sauravhathi
        if (strcmp(current_string, the_string) == 0)
        {

            if ((i == 0 && ((*input_string)[i + 3] == '\0' || (*input_string)[i + 3] == ' ')) || (i > 0 && (*input_string)[i - 1] == ' ' && ((*input_string)[i + 3] == '\0' || (*input_string)[i + 3] == ' ')))
            {

                i += 3;
                continue;
            }
        }
        new_string[new_string_idx] = (*input_string)[i];
        new_string_idx += 1;
    }

    new_string[new_string_idx] = '\0';
    *input_string = new_string;
}
int main()
{
    char *input_string = (char *)malloc(MAX_LIMIT * sizeof(char));
    printf("Enter the string: \n");
    // sauravhathi
    scanf("%[^\n]%*c", input_string);
    removeOccurrenceOfTheWord(&input_string);
    printf("The resultant string is: \n");
    printf("%s", input_string);
    return 0;
}
string = input("Enter the string\n ")
string = string.replace("the", "")
#sauravhathi
print("Result string is\n", string)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *