Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

removing spaces – pass by reference

prompt – c++

Write a program that removes all spaces from the given input.

Ex: If the input is: "Hello my name is John." the output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

HellomynameisJohn.
Your program must define and call the following function. The function should return a string representing the input string without spaces.
void RemoveSpaces(string &userString)

issue – i believe my code is correct; i’m just not very clear on the concept of pass by reference so my code is wrong in terms of my assignment. that’s why my output still shows up as string with spaces in my submission.

how would i write this using pass by reference?

my code –

#include <iostream>
using namespace std;

void RemoveSpaces ( string &userString )
{
   unsigned int i ; 
   
   for ( i = 0 ; i < userString.size() ; i ++ )
   {
      if ( userString.at(i) != ' ' )
      {
         cout << userString.at(i) ;
      }
   }
} 

int main() {
   
   string userInputString ;
   
   getline ( cin, userInputString ); 
   
   RemoveSpaces ( userInputString ) ;
   
   cout << userInputString ; 

   return 0;
}

for pass by reference i had thought that userString would be "updated" in the function and output as the updated version?

>Solution :

It seems to be a common newbie confusion. Printing gets confused with other concepts. If a function prints something, then the function is ‘returning’ what is printed. This is completely untrue, printing is printing, nothing else.

If you want to write a function that removes spaces from a string, then that is what the function must do. Somehow a new string without the spaces must be created. How that function returns the modified string is a side issue.

Here’s a function that removes spaces from a string.

void RemoveSpaces ( string &userString )
{
   string temp;
   for (size_t i = 0 ; i < userString.size() ; i ++ )
   {
      if (userString.at(i) != ' ' )
          temp.push_back(userString.at(i));
   }
   userString = temp;
}

This function works by looking for the non-spaces in userString and adding them to a new string temp. This is the string without spaces. Then at the end of the function it assigns this string back to userString. Because userString has been passed by reference this assignment modifies userInputString in main. That’s the meaning of pass by reference. Changes to userString actually change the string that is being referred to.

It is possible to write a function that modifies userString directly, but that is more complicated code, so I chose to do it this way with a second string temp.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading