1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
fenix001 [56]
3 years ago
6

Write a function add_spaces(s) that takes an arbitrary string s as input and uses recursion to form and return the string formed

by adding a space between each pair of adjacent characters in the string. If the string has fewer than two characters, it should be returned without any changes. Here are three examples: >>> add_spaces('hello') result: 'h e l l o' >>> add_spaces('hangman') result: 'h a n g m a n' >>> add_spaces('x') result: 'x' This function is somewhat similar to the the replace function from lecture, because it needs to recursively create a new string from an existing string. However, your function will be simpler, because it won’t need to decide what to do after the recursive call returns.
Computers and Technology
2 answers:
kvasek [131]3 years ago
5 0

Answer:

The solution code is written in Python:

  1. def add_spaces(s):
  2.    if len(s) < 2:
  3.        return s  
  4.    else:
  5.        return s[0] + " " + add_spaces( s[1 : ] )

Explanation:

Recursive function is a function that will call itself within the same function.

Let create a function named add_spaces() that take one input string, s (Line 1).

Next, create an if condition to check if the length of the input string is less than 2 (this means if the string has only one character), return the current string (Line 2-3).

Otherwise, it should return the first character of string, s[0] concatenated with a single space " " and concatenated again with the return output from the recursive calling add_spaces() that take s[1: ] as input parameter (Line 4-5). Please note s[1: ] is an expression we get the substring of the current string from position 1 till the end of string.

PtichkaEL [24]3 years ago
5 0

Answer:

C++.

#include <iostream>

#include <string>

using namespace std;

///////////////////////////////////////////////////////////////////

string add_spaces(string input) {

   if (input.length() < 2)

       return input;

   else {

       string minus_input = input.substr(0, input.length()-1);

       return add_spaces(minus_input) + " " + input[input.length()-1];

   }

}

///////////////////////////////////////////////////////////////////

int main() {

   cout<<add_spaces("hangman");

   return 0;

}

///////////////////////////////////////////////////////////////////

Explanation:

A Recursive function calls itself inside its body. To prevent it from running forever, there's a break condition inside recursive functions.

In our example it was a single character string.

We use recursion to solve problems by breaking them into smaller pieces.

In our example, we broke down the original string from the last character, till it only has its starting character. When the last character was encountered, we returned as it is in the break condition.

From there onward, all characters were getting separated by a space, till we reach the first add_spaces function call, where we return the full string with space between them.

You might be interested in
Which of the following represents a transition?
timama [110]

Explanation:

C.

D.

395 - 44th Street Long B.

Ο Α

O

B

С

OD

8 0
3 years ago
write a program that inserts on the p position of a string read from the keyboard also another string read from the keyboard
vovangra [49]

Answer:

px

Explanation:

6 0
3 years ago
Batter boards (if properly offset) will allow for the end user to continually re-string the layout to double check accuracy with
natulia [17]

Answer:

The given statement is "True".

Explanation:

  • Batter panels seem to be platform frames that are used to temporarily suspend foundations plan threads. These same batter board members look like barriers once built.
  • Its positioning is important for creating a foundation precisely as the plans state since some components of their development would have to be accurate. Placed correctly batter panels guarantee that the boundaries seem to be at the appropriate temperatures as well as positions.
4 0
3 years ago
A technician wants to create a new partition on a new additional hard drive. what tool should be used?
Vitek1552 [10]
A Disk Defragmenter should be used
7 0
3 years ago
content from other sources can be embedded into it linked to a photoshop document from another source
Ann [662]

Answer:

PLEASE MARK MY ANSWER BRAINLIEST

Explanation:

Create embedded Smart Objects

(Photoshop) Choose File > Place Embedded to import files as Smart Objects into an open Photoshop document.

Choose File > Open As Smart Object, select a file, and click Open.

(Photoshop CS6) Choose File> Place to import files as Smart Objects into an open Photoshop document.

3 0
3 years ago
Other questions:
  • "Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequen
    5·1 answer
  • Bar-code readers are typically used to track large numbers of inventory items, as in grocery store inventory and checkout, packa
    14·1 answer
  • Why should you use a named constant for the size of an array?
    9·1 answer
  • Which internet site end in .com​
    10·2 answers
  • What age group is experiencing the most growth in social media
    13·1 answer
  • Write the name of the tab, the command group, and the icon that you need to use to justify text
    9·1 answer
  • Select all that apply.
    5·1 answer
  • The advantage of using a belt drive over chain drive. vice versa
    12·1 answer
  • 334. Universal Containers uses a custom field on the account object to capture the account credit status. The sales team wants t
    12·1 answer
  • Computer identity theft differs from theft in the real world in what major way?
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!