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;
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
}
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
constchar* pStr = "A big day on the farm.";// can't write to this
constint sz = strlen(pStr);
char str[sz+1];// can write to this. Make as copy of pStr.
constchar* 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!