开发者

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 != ' ')
   co开发者_如何转开发unt++;
  {
   *p++;
  }
 }
 return count;


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);


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;
}   


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;


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");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜