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

Write a C program for the recursive algorithm that removes all occurrences of a specific character...
Write a C program for the recursive algorithm that removes all occurrences of a specific character from a string. (please comment the code)
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of...
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C .4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
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.
Write a C++ program that uses a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates...
Write a C++ program that uses a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates in a C++ string. Consecutive duplicates are a pair of duplicate English alphabets sitting next to each other in the input string. Example: "AA", "KK", etc., are all consecutive duplicates. This function will internally run as many iterations as needed to remove all consecutive duplicates until there is either no consecutive duplicates left, or the string becomes empty (in which the function returns "Empty"...
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...
** * Write a recursive function that removes the first k even numbers * from the...
** * Write a recursive function that removes the first k even numbers * from the stack. If there are less than k even elements in the stack, * just remove all even elements. Do not use any loops or data structures * other than the stack passed in as a parameter. * @param stack * @param k * @return Returns the number of elements removed from the stack. */ public static int removeEvenNumbers(Stack<Integer> stack, int k) { return 0;...
Write a recursive program in C++ to compute the determinant of an NxN matrix, A. Your...
Write a recursive program in C++ to compute the determinant of an NxN matrix, A. Your program should ask the user to enter the value of N, followed by the path of a file where the entries of the matrix could be found. It should then read the file, compute the determinant and return its value. Compile and run your program.
Write a recursive program in C++ to compute the determinant of an NxN matrix, A. Your...
Write a recursive program in C++ to compute the determinant of an NxN matrix, A. Your program should ask the user to enter the value of N, followed by the path of a file where the entries of the matrix could be found. It should then read the file, compute the determinant and return its value. Compile and run your program.
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT