Question 7: Write a C++ program that takes input a string and a character and return a string after removing the instances of character from the string.
Question 7: Write a C++ program that takes input a string and a character and return a string after removing the instances of character from the string.
/*
Name: Mudassar Ali
Roll: BCSF15M045 [Morning]
Assignment: No# 05
Teacher: Miss Sadia Shahzad
*/
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout<<"This programme will remove the space and also entered character from string!!\n\n";
char str [50]; // To hold the contents of
char ch; // To store a single character.
cout<<"Enter the String: ";
cin.getline(str,50); // Inputting the String with spaces.
cout<<"Enter a Character: ";
cin>>ch; // Inputting the single character.
for(int i = 0; str[i] !='\0';i++) // Loop for removing the entered letter.
{
if(str[i]==ch ||str[i]==' ') // IF character or space occurs then they will be removed.
{
str[i] = 7; // Here '7' is an ACII Code not 'Number 7' . To place a where ch and space remove.
// This 7 will also make a Bell Sound.
}
}
cout<<"New String is: "<<str; // Printing the string after taking actions on it.
return 0;
}
No comments: