In: Computer Science
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a specific character from a string
b. Write the pseudocode for the program.
(a) Program:
#include <bits/stdc++.h> //includes all header files
using namespace std;
#include<iostream>
void remove(char a[], int pos)
{
while(*(a + pos) != '\0')
*(a + pos)=*(a + (++pos));
return;
}
void search(char a[], char ch, int pos)
{
if (*(a + pos)=='\0')
return;
else
{
if (*(a + pos) == ch)
remove(a, pos);
if(*(a + pos) == ch)
search(a, ch, pos);
else
search(a, ch, ++pos);
}
}
int main()
{
char s[50], ch;
cout<<"Enter the string: ";
cin.getline(s, 50);
cout<<"Enter the character to remove: ";
cin>>ch;
search(s, ch, 0);
cout << s;
return 0;
}
(b) Pseudocode:
remove(a[], ipos):
while *(a + pos) != '\0' :
*(a + pos)=*(a + (++pos));
return;
search(a[], ch, pos):
if *(a + pos)=='\0' :
return;
else :
if *(a + pos) == ch :
remove(a, pos);
if *(a + pos) == ch :
search(a, ch, pos);
else :
search(a, ch, ++pos);
main():
Enter string and store in s
Enter character to be removed and store in ch
Call searchChar(s, ch, 0);
Print s