Question

In: Computer Science

1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a...

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.

Solutions

Expert Solution

(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


Related Solutions

Create a program in C++ implementing the LinkedStack 7. Write a client that removes all negative...
Create a program in C++ implementing the LinkedStack 7. Write a client that removes all negative numbers from a stack of int objects. If the original stack contained the integers 30, –15, 20, –25 (top of stack), the new stack should contain the integers 30, 20.
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in zyBooks sections 9.4 and 9.5. Prompt the user for the location of a sequence of numbers, via an external file or data entry by the user. If you choose data entry, prompt the user for the number of values and read them into a data structure of your choice. Then use the mergesort algorithm to sort them in ascending order. Finally, prompt for a...
Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (%...
Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (% & loop) 1. Euclid’s Algorithm An alternative of the Euclidean algorithm for finding greatest common divisors (GCD) is repeatedly performing the modulo operation on two numbers until the remainder is 0. Here is the pseudocode for find the GCD of two positive numbers a and b using the Euclidean algorithm :while b ≠ 0 temp = b b = a mod t a =...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT