Deleting Spaces from Cstring and returning number deleted (pointers, not array notation)

I am completely lost on how to finish this particular build. I have been tasked with building a function that will take a user input string, pass the argument using pointers (no array notation allowed) delete the spaces, and the return the number of spaces deleted. Currently I have two builds. One of which, deletes the spaces, and prints it correctly, but instead gives me the length of the string.(CODE A)

The other gives me the correct amount of spaces deleted, but doesn't print the new string without spaces, or if it does, it replaces them to be spaces. (CODE B)

{

Using any of the following is unacceptable for this build.
using MS VS 2010

global variables
array notation (i.e. anything with [ ]) except for declaring your C String in main
cin/cout in any function other than main
goto, break (other than in a switch), or continue statements}

Here are the respective codes, what am I missing? or how could I combine them to get the desired result


CODE A:

#include <iostream>
#include <cstring>

using namespace std;
int RemoveSpaces(char* inputStr);



int main ()

{

char str[100];
cout << "Enter a scentence and I will remove the spaces." << endl;
cin.getline(str,99);
cout<< "I removed: " << RemoveSpaces(str) << " space(s) from the scentence."<<endl;
cout<<"It now reads without spaces: " << endl;
cout << *str << endl;
return 0 ;

}

int RemoveSpaces(char* inputStr)
{
char* s2 = inputStr;
int length=strlen(s2);
int numofSpaces=0;


for (length; *s2; ++s2)
{
if (*s2 != ' ')
{

*inputStr = *s2;
numofSpaces++;
}

}

*inputStr = 0;


return numofSpaces;
}




CODE B:
#include <iostream>
#include <cstring>

using namespace std;
int RemoveSpaces(char* inputStr);



int main ()

{

char str[100];
cout << "Enter a scentence and I will remove the spaces." << endl;
cin.getline(str,99);
cout<< "I removed: " << RemoveSpaces(str) << " space(s) from the scentence."<<endl;
cout<<"It now reads without spaces: " << endl;
cout << *str << endl;
return 0 ;

}

int RemoveSpaces(char* inputStr)
{
char* s2 = inputStr;
int length=strlen(s2);
int numofSpaces=0;


for (length; *s2; ++s2)
{
if (*s2 == ' ')
{
numofSpaces++;
*inputStr = *s2;

}

}

*inputStr = 0;


return numofSpaces;
}

closed account (D80DSL3A)
You're fairly close with code A. Couple of problems:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int RemoveSpaces(char* inputStr)
{
char* s2 = inputStr;
int length=strlen(s2);
int numofSpaces=0;


for (length; *s2; ++s2)
{
if (*s2 != ' ')
{

*inputStr = *s2;// good, but inputStr never increments. keeps overwriting 1st element only
numofSpaces++;
}

}

*inputStr = 0;// writes '\0' to 1st element. want '\0' in last.


return numofSpaces;// can calculate correct # from this though
}

Here it's a bit more cleaned with corrections:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int RemoveSpaces(char* inputStr)
{
    char* s2 = inputStr;
    int length=strlen(s2);
    int numofSpaces=0;

//    for (length; *s2; ++s2)
    for (; *s2; ++s2)// this will work, but Declaring & init s2 here preferred
    {
        if (*s2 != ' ')
        {
       //     *inputStr = *s2;// overwrites 1st element only
            *(inputStr++) = *s2;// overwrite with non-blank char then increment pointer
            numofSpaces++;
        }
    }

//    *inputStr = 0;// now this puts '\0' at the end
    *inputStr = '\0';// although = 0 works, fairly sure = '\0' is preferred

    return length - numofSpaces;
}

That code for copy + increment pointer *(inputStr++) = *s2; is a bit confusing but it may be what your prof is looking for if he wants all this done in "pointer notation".
edit: If that's too confusing, I'm sure it would be OK to do it in 2 steps instead:
1
2
*inputStr = *s2;// copy
inputStr++;// increment 


Also, in your main() you output str as cout << *str << endl; which will output only the 1st char in str. You want cout << str << endl; instead.

Want to see some messy pointer notation stuff? Here's how I got my char[] array filled in main():
1
2
3
4
5
6
7
8
    const char* pStr = "A big day on the farm.";// can't write to this
    const int sz = strlen(pStr);
    char str[sz+1];// can write to this. Make as copy of pStr.

    const char* pSrc = pStr;// pointers as iterators!
    char* pDest = str;
    while( *pSrc ){ *(pDest++) = *(pSrc++); }// copy element pointed to then increment both pointers
    *pDest = '\0';// seal with a kiss! 

Ah pointers, fun stuff!
Last edited on
Sorry for the late reply! I was able to figure it out with your help! I am sure I will be back soon!
Thanks again!
Topic archived. No new replies allowed.