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 program for C++ program to delete words from the sentence.
Copy the below C program and execute it with the help of Turbo C compiler. At the end of this program, We have shared the output of this 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 52 53 54 | #include<iostream.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]; cout<<"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'; cout<<"Enter a word to be delete : "; cin>>word; /* Comparing the string with the given 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--; } } } cout<<"The new String after deleting the word : \n"; for (i=0; i<k+1; i++) { cout<<str1[i]<<" "; } getch(); } |
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.