Home > Net >  removing spaces - pass by reference
removing spaces - pass by reference

Time:11-17

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:

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?

CodePudding user response:

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.

CodePudding user response:

First some remarks:

  • avoid using namespace std. It can load too many identifiers into the current names which can lead to name clashes. Better to prefix standard identifiers with std:: or explictely import the ones you will use ofter
  • you are using a version of getline that is declared in the <string> standard header without including that header. It worked in your implementation because C allows implicit includes for standard headers, but failed in mine, because iostream did not automatically load string...

Now for your real question. erase can be used here because it can erase characters in a string. You just have to browse the string with an iterator and erase all space characters. Code could be:

void RemoveSpaces(string& userString)
{
    for (auto it = userString.begin(); it != userString.end();) { // no automatic increment here
        if (' ' == *it) {
            it = userString.erase(it);  // erase a space character
        }
        else {
              it;                       // or skip to next one
        }
    }
}

CodePudding user response:

in the function RemoveSpaces, you are printing the srting without spaces but not updating it. So for example the string is "This is an example" it would just print "Thisisanexample" but the value of the string still remains the same. You will have to update the value of string in the function inorder to update the original string value, pass by reference doesn't automatically do it.

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

resize(count) is added cause for example orginial string - "This is an example" size - 18 updated string - "Thisisanexampleple" size - 18 This happens because updated string has a size of 15("Thisisanexample") but original has size of 18 so it will take 18-15 = 3 characters from original string and keep it in updated string. hence you need to resize the updated string to 15 i.e. the count variable which tracks size of updated string.

  • Related