vsmile
vsmile

Reputation: 11

I wrote a c++ code to strip out the spaces of a sentence but when i try to print it it won't print past the first word

# include <iostream>
# include <ctime>

using namespace std;

int stripWhite(char *str);

int main ()
{

 char str[50];
 cout << "Enter a sentence . " << endl;
 cin >>str;
 cout << "Your sentence without spaces is : " << endl;
 cout << (str) << endl; // This is my problem. The sentence only prints the first word

 stripWhite(str);
 cout << "There were " << stripWhite(str) << " spaces." << endl;
 return 0;
}
int stripWhite(char *str)
{
 char *p = str;
 int count = 0;
 while (*p)
 {
  if (*p != ' ')
   count++;
  {
   *p++;
  }
 }
 return count;

Upvotes: 1

Views: 607

Answers (4)

sum1stolemyname
sum1stolemyname

Reputation: 4552

std::cin treats spaces as end of string indicators.

In order to get the full sentence use std::getline. since this expects a std::string as one of its parameters, you will have to adjust your stripWhite-function accordingly:

# include <iostream>
# include <string>

using namespace std;

int stripWhite(string str); //change the formal parameter's type

int main ()
{

 string str;
 cout << "Enter a sentence . " << endl;
 getline(cin, str,'\n'); //use getline to read everything that has been entered till the press of enter
 cout << "Your sentence without spaces is : " << endl;
 cout << (str) << endl; // This is my problem. The sentence only prints the first word

 stripWhite(str);
 cout << "There were " << stripWhite(str) << " spaces." << endl;
 system("pause");
 return 0;
}

int stripWhite(string str)
{

 int count = 0;
 char* p = str.c_str;
 while (*p)
 {
  if (*p != ' ')
   count++;
  {
   *p++;
  }
 }
 return count;
}   

Upvotes: 1

Matthieu M.
Matthieu M.

Reputation: 299800

Behold Boost String Algorithms and more particularly the replace/erase routines.

# include <iostream>
# include <string>

size_t stripWhiteSpaces(std::string& str)
{
  size_t const originalSize = str.size();
  boost::erase_all(str, ' ');
  return originalSize - str.size();
}

int main ()
{

  std::string str;
  std::cout << "Enter a sentence . \n";
  getline(std::cin, str);

  size_t const removed = stripWhiteSpaces(str);
  std::cout << "Your sentence without spaces is :\n";
  std::cout << (str) << '\n';

  std::cout << "There were " << removed << " spaces.\n";
  system("pause");
}

Upvotes: 0

icecrime
icecrime

Reputation: 76755

As pointed out by others, you should use std::getline instead of cin >> str.

However, there are multiple other problems in the code you provided.

  • Why use char array when you could use std::string ? Why are you so sure that 50 characters will be enough ?
  • Your stripWhite function doesn't seem to strip anything : you count the number of non-space characters, but you are not actually removing anything. Note that if you switch to std::string instead of plain of char arrays, you could use a standard algorithm to do the job (on the top of my head, I guess std::remove would be appropriate)
  • Assuming that stripWhite did actually modify the input string, why would you want to call it twice from your main ? If the goal is to strip the string in the first place, and then print the number of removed space, make stripWhite return the number of removed spaces and store this result in the main.

For example :

const int nbSpacesStripped = stripWhite(str);
cout << "There were " << nbSpacesStripped << "spaces." << endl;

Upvotes: 0

Cooper6581
Cooper6581

Reputation: 241

If you don't want to replace your function with the C++ string type, you can use cin.getline to get a c string (char array)

cin.getline(str, 50);

Upvotes: 1

Related Questions