- Home
- Course Category
- Course Result
- Course Detail
Hot Line: 0124-2383121
Course Introduction
Programs for practice
Following C++ program ask to the user to enter a sentence then ask to enter a word to be delete from the sentence, then display the new sentence after deleting the given word:
Source Code: Print Hello World
/* C++ Program - Delete Words from Sentence */
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int i, j = 0, k = 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();
}
Output
Enter the String : welcome to blazingminds
Enter a word to be delete :to
The new String after deleting the word : welcome blazingminds