Adding spaces between each character in a string

Hello! I have been struggling with this program, the prompt and the code I have created thus far is below:

Create a program that includes a function called spaced that takes a string and returns a string with spaces beteen each character (including spaces). For example, given “hello” as the input, the function should return “h e l l o ”, which is the spaced string of “hello”.

The main function should prompt the user to input a string until the user types “Q” For each string input call the function with the string and display the result. Note that the user input string may contain white spaces.

Example input/output sessions:

Hello
H e l l o
Q

C++ is fun!
C + + i s f u n !
Q

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
using namespace std;

string Spaced(string userString) {
	string spacedString;
	int stringSz = userString.size();

		for (int i = 0; i <= stringSz; i++) {}
			spacedString = userString.substr(i, i) + " ";
	}
return spacedString;
}

int main() {
	string userString;
	string spacedString;
	int i;

	getline(cin, userString);

//	while (userString != "Q") {
		cout << userString << endl;
	}

	system("pause");
	return 0;
}
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

std::string Spaced(std::string userString);

int main()
{
   std::cout << "Enter a string: ";
   std::string userString;
   std::getline(std::cin, userString);

   std::string spacedString = Spaced(userString);
   std::cout << "\nThe spaced string is:\n" << spacedString << '\n';
}


std::string Spaced(std::string userString)
{
   std::string spacedString;

   for (auto itr : userString)
   {
      spacedString += itr;
      spacedString += ' ';
   }

   return spacedString;
}


Enter a string: C++ is fun

The spaced string is:
C + +   i s   f u n
Hello juliabrushett,

I would include the header file "<cctype>" and in your function "Spaced" make use if the function "isspace" in the for loop. I am thing of something like:
1
2
3
for(...)
    if (!isspace(userString[i]))
        spacedString+=userString[i];


I will have to go test this, but that is my thought for now. You could change this to not use the built in functions in which case it would follow thae same type of concept.

Hope that helps,

Andy

P.S. As I worked on the program I ended up using the same concept as FurryGuy did in his for loop except I used the for loop that you started with.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;


string spaced( string str )
{
   if ( str == "" ) return str;

   string result( 2 * str.size() - 1, ' ' );
   for ( int i = 0, j = 0; i < str.size(); i++, j+=2 ) result[j] = str[i];
   return result;
}


int main()
{
   string line;
   while ( true )
   {
      cout << "Enter a string: ";   getline( cin, line );
      if ( line == "Q" ) return 0;
      cout << spaced( line ) << '\n';
   }
}

Enter a string: C++ is fun
C + +   i s   f u n
Enter a string: Q
Topic archived. No new replies allowed.