In C++, write a method that remove a character from a C string using pointers
Question
Write a function named deleteG
that accepts one character pointer as a parameter and returns no value. The parameter is a C string. This function must remove all of the upper and lower case 'g' letters from the string. The resulting string must be a valid C string.
Your function must declare no more than one local variable in addition to the parameter; that additional variable must be of a pointer type. Your function must not use any square brackets and must not use any of the <cstring>
functions such as strlen
, strcpy
, etc.
Explanation
The deleteG method will have the array s of data type ‘char’ as the only parameter. When an array in C++ is passed into a parameter, what it actually is in the method is the memory address of the first element of the array.
The pointer ‘p’ is declared in the deleteG method to ‘s’. When declared to as the array, it is the same as declaring ‘p’ as ‘&s[0]’.
The while loop will continue to iterate until a null terminator is found. Within the while loop, an if statement is declared to check if the current value where the pointer is pointing at is a ‘G’ or ‘g’. If so, another while statement is declared to shift the array.
Code
#include <iostream>using namespace std;void deleteG(char s[]) {