In this tutorial you will learn about the C Program to Delete Word from String and its application with practical example.
C Program to Delete Word from String
In this tutorial, we will learn to create a C program that will Delete Word from String in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in c programming.
- Conditional statements in C programming.
- String functions of c programming.
Program to Delete Word from String
As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First, we will take the input string from the user. Then we will Delete the Word from String using the while loop.
With the help of this program, we can Delete Word from String.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declare the variables for the program. 2. Take the input string from the user. 3. <strong>Delete Word from String</strong>. 4. Print the output. 5. End the program. |
Program to Delete Word from String:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<stdio.h> #include<string.h> int main() { //declaring the variable for the program char str[100], word[20]; int i, j, ls, lw, temp, chk=0; //Getting input string from the user printf("Enter the String: "); gets(str); //Taking the removing word from the user printf("Enter a Word: "); gets(word); //Using the string functions ls = strlen(str); lw = strlen(word); for(i=0; i<ls; i++) { temp = i; for(j=0; j<lw; j++) { if(str[i]==word[j]) i++; } chk = i-temp; if(chk==lw) { i = temp; for(j=i; j<(ls-lw); j++) str[j] = str[j+lw]; ls = ls-lw; str[j]='\0'; } } //Printing the output string with out the taken word printf("\nNew String = %s", str); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- str[100] = it will hold the string value.
- word[20] = it will hold the string value.
- i = it will hold the integer value.
- j = it will hold the integer value.
- chk = it will hold the integer value.
- temp = it will hold the integer value.
- ls = it will hold the integer value.
- lw = it will hold the integer value.
Taking the input string from the user.