Answer to Question nullterm-3
// numNonblanks(s) returns the number of nonblank
// characters in null-terminated string s.
int numNonblanks(const char* s)
{
int count = 0;
for(int i = 0; s[i] != '\0'; i++)
{
if(s[i] != ' ')
{
count++;
}
}
return count;
}
// removeBlanks(s) returns a string (allocated using new)
// that holds all of the non-blank characters in s.
char* removeBlanks(const char* s)
{
int n = numNonblanks(s);
char* t = new char[n + 1];
int j = 0;
for(int i = 0; s[i] != '\0'; i++)
{
if(s[i] != ' ')
{
t[j] = s[i];
j++;
}
}
t[j] = '\0';
return t;
}