Thursday, September 19, 2019

Delete a Specific Word From a Sentence in C

Write a program to remove a word from a sentence given by the user using the C programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My personal website is http://www.jakerpomperada.com.



Sample Program Output


Program Listing

delete.c

/*delete.c
 Author    : Mr. Jake Rodriguez Pomperada,BSCS,MAED-IT
 Date      : December 1, 2018  Saturday 9:40 AM
 Location  : Bacolod City, Negros Occidental
 Website   : http://www.jakerpomperada.com
 Emails    : jakerpomperada@jakerpomperada.com
             jakerpomperada@gmail.com
             jakerpomperada@yahoo.com
             jakerpomperada@aol.com
*/
#include <stdio.h>
#include <stdlib.h>
int search(char[], char[]);
int delete_word(char[], char[], int);
int main()
{
    char str[80], word[50];
    int index=0;
printf("\n\n");
  printf("\t\tDelete a Specific Word From a Sentence");
printf("\n\n");
    printf("\tEnter a Sentence  :  ");
    gets(str);
    printf("\n");
    printf("\tEnter Word to Delete : ");
    gets(word);
    printf("\n");
    index = search(str, word);
    if (index !=  - 1)
    {
        delete_word(str, word, index);
        printf("\tString without word: %s", str);
    }
    else
    {
        printf("\n");
printf("\tThe word not present in the string.");
    }
    printf("\n\n");
    printf("\tThank you for Using This Software.");
    printf("\n\n");
    system("PAUSE");
}
int search(char str[], char word[])
{
    int l, i, j;
    for (l = 0; word[l] != '\0'; l++);
    for (i = 0, j = 0; str[i] != '\0' && word[j] != '\0'; i++)
    {
        if (str[i] == word[j])
        {
            j++;
        }
        else
        {
            j = 0;
        }
    }

    if (j == l)
    {
     return (i - j);
    }
    else
    {
        return  - 1;
    }
}

int delete_word(char str[], char word[], int index)
{
    int i, l;
    for (l = 0; word[l] != '\0'; l++);
    for (i = index; str[i] != '\0'; i++)
    {
        str[i] = str[i + l + 1];
    }
}


No comments:

Post a Comment