Question

In: Computer Science

Please Write Code in C++ and include the correct #include <header> and not a catchall such...

Please Write Code in C++ and include the correct #include <header> and not a catchall such as bits/stdc:

Write a recursive, string-valued function, replace, that accepts a string and returns a new string consisting of the original string with each blank replaced with an asterisk (*)

Replacing the blanks in a string involves:

  • Nothing if the string is empty
  • Otherwise:
    • If the first character is not a blank, simply concatenate it with the result of replacing the rest of the string
    • If the first character is a blank, concatenate an * with the result of replacing the rest of the string

Solutions

Expert Solution

Code

#include<iostream>
using namespace std;

string rec(string s,int c)
{
    if(s.empty() && c==0) //if c==0 then string is processing for first time and empty() defines empty string hence return nothing
        return "Nothing";
    if(s.empty() && c!=0) //c!=1 defines non empty string passed hence no need to add "nothing" at end hence return ""
        return "";
    c++;
    if(s[0]==' ')
        return "*"+rec(s.substr(1),c);
    return s[0]+rec(s.substr(1),c);
}
int main()
{
    string s; 
    cout<<"Enter the string:\n";
    getline(cin,s); //get input string from user
    int c=0; //helps to decide return nothing or ""
    string res=rec(s,c);
    cout<<"Modified string is:\n"<<res;
    return 0;
}

Terminal Work

.

​​​​​


Related Solutions

Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include...
Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include <string.h> #include <ctype.h> int main(int argb, char *argv[]){ char ch; int upper=1; if(argb>1){ if(strcmp(argv[1],"-s")==0){ upper=1; } else if(strcmp(argv[1],"-w")==0){ upper=0; } } while((ch=(char)getchar())!=EOF){ if(upper){ printf("%c",toupper(ch)); } else{ printf("%c",tolower(ch)); } } return 0; } ======================================================== #include <stdio.h> #include <stdlib.h> int calcRange(int array[],int size){ int max=array[1]; int min=array[0]; int a=0; for(a=1;a<size;a++){ if(array[a]>max){ max=array[a]; } } for(a=1;a<size;a++){ if(array[a]<min){ min=array[a]; } } printf("%d-%d=%d\n",max,min,max-min); } int main(int arga, char **argv){...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header containing your name and a brief description of the program Output prompting the user for the following inputs: Name as a string Length of a rectangle as a double Width of a rectangle as a double Length of a square as an int After accepting user input, the program outputs the following: User name Area of a rectangle with dimensions matching the inputs Area...
using correct c++ syntax, in one part write a short header file with only one constructor...
using correct c++ syntax, in one part write a short header file with only one constructor and one private variable. in another part write the function definition for the same constructor of the header file you just wrote. using a correct c++ syntax, write only the top line of a derived header file that uses a base class. write the function definitions of two constructors in this derived class
Write a C program (using Code::blocks) that outputs a formatted header line and creates 10 children...
Write a C program (using Code::blocks) that outputs a formatted header line and creates 10 children processes each of which displays a line of the output as shown below. Notice the values of Child no and x, they have to be the same as shown here while the processes PIDs values are based on the what your system assigns to these processes. Child    PID        PPID      X 0           13654    13653    5 1           13655    13653    10 2           13656    13653    15 3           13657   ...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
**C++ program** Please provide a header file(bubble_sort.h) and a program file(bubble_sort.cpp). Also include notes to explain...
**C++ program** Please provide a header file(bubble_sort.h) and a program file(bubble_sort.cpp). Also include notes to explain code and output screenshots. Please use Bubble Sort code provided. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h>...
Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h> #include <semaphore.h> #include <pthread.h> using namespace std; #define NRO 6 // Número de coches //Puente declarado con matriz y valor entero void Puente(string array, int value); // Variable global int Norte = 1; int Sur = 1; sem_t mutex1; //Coche al norte void* NorteC(void* arg){ sem_wait(&mutex1); string array = "En el lado Norte "; // Norte cout<<array<<"del puente, el coche #"<<Norte<<" puede cruzar el...
Can someone please write me a header file in c++ using the quadratic probing hash method...
Can someone please write me a header file in c++ using the quadratic probing hash method that will work with this program? #include "hash.h" #include <algorithm> #include <cstdlib> #include <ctime> #include <iostream> #include <list> using namespace std; const size_t KEYS_COUNT = 1000; // Number of keys to generate for testing string random_key(); class HashCheck { private: size_t count; Hash hash; public: HashCheck(Hash& h) { count = 0; hash = h; } void operator() (const string key) { if (hash[key] !=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT