C++ Program to Delete Given Character From String
Write C++ Program to Delete Given Character From String
// CPP Program to Delete Given Character From String #include <iostream> #include <string.h> using namespace std; int main() { char S[] = "Hello and Welcome to CodeCrucks", ch; int n; cout << "Original String :--> " << S; cout << "\nEnter Character to Remove :--> "; cin >> ch; n = strlen(S); int i = 0, flag = 0; while(i < n) { if(S[i] == ch) { flag = 1; int j = i; while(j < (n - 1)) { S[j] = S[j + 1]; //shift the elements to 1 step left to the index i; j++; } n--; i--; } i++; } S[i] = NULL; if(flag == 0) { cout << "Element is not present in the array"; } else { cout << "\nString After Deleting " << ch << " :--> " << S; } return 0; }
Output:
Original String :--> Hello and Welcome to CodeCrucks
Enter Character to Remove :--> e
String After Deleting e :--> Hllo and Wlcom to CodCrucks