In this program, we are going to share a C program to delete words from the sentence. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program to delete words from the sentence with the output.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.
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 40 41 42 43 44 45 46 47 48 49 50 51 | #include<stdio.h> #include<conio.h> #include<string.h> void main() { clrscr(); int i, j = 0, k = 0, count = 0; char str[100], str1[10][20], word[20]; printf("Enter the String : "); gets(str); /* Converting the string into 2D Array */ for (i=0; str[i]!='\0'; i++) { if (str[i]==' ') { str1[k][j] = '\0'; k++; j=0; } else { str1[k][j]=str[i]; j++; } } str1[k][j] = '\0'; printf("Enter a word to be delete : "); scanf("%s", word); for (i=0; i<k+1; i++) { if (strcmp(str1[i], word) == 0) { for (j=i; j<k+1; j++) { strcpy(str1[j], str1[j + 1]); k--; } } } printf("The new String after deleting the word : \n"); for (i=0; i<k+1; i++) { printf("%s ", str1[i]); } getch(); } |
Enter the String : This is an example of deleting words from sentence sentence.
Enter a word to be delete : sentence
The new String after deleting the word :
This is an example of deleting words from sentence.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.