Remove character
#include <iostream>
#include <cstring>
using namespace std;
#include "solution.h"
For a given a string(str) and a character X, write a function to remove all the occurrences of X from the given string.
The input string will remain unchanged if the given character(X) doesn't exist in the input string.
Input Format:
Output Format:
Note:
Constraints:
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
#include<cstring>
int lenght(char str[]){
int count=0;
for (int i=0;str[i]!='\0';i++){
count++;
}
return count;
}
void trimSpaces(char str[]) {
// Write your code here
int i = 0, j = 0;
while (str[i])
{
if (str[i] != ' ')
str[j++] = str[i];
i++;
}
str[j] = '\0';
}
void removeAllOccurrencesOfChar(char input[], char c) {
// Write your code here
for(int i=0;i<strlen(input);i++){
if(input[i]== c){
input[i]=' ';
}
}
trimSpaces(input);
}
int main() {
int size = 1e6;
char str[size];
cin.getline(str, size);
char c;
cin >> c;
removeAllOccurrencesOfChar(str, c);
cout << str;
}
Comments
Post a Comment