C++ 函数删除句点和空格

C++ 函数删除句点和空格,c++,C++,我需要创建一个函数,该函数将删除句子和空格中的所有句点 它只是打印出句子的第一个单词,而不是整个句子。如果我输入“你好,我的名字是”,它将只打印“你好”,但删除句号。我还需要删除空格这里是我的代码到目前为止 #include<string> #include<iostream> #include<string> #include<algorithm> #include<vector> #include<fstream> u

我需要创建一个函数,该函数将删除句子和空格中的所有句点

它只是打印出句子的第一个单词,而不是整个句子。如果我输入“你好,我的名字是”,它将只打印“你好”,但删除句号。我还需要删除空格这里是我的代码到目前为止

#include<string>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<fstream>

using namespace std;
void removePun(char len, string str);
void removeSpaces(char str);
int main()
{
  string sentence;
  char len;
  getline(cin,sentence);
  removePun(len,sentence);
  removeSpaces(len);

  return 0;

}

void removePun(char len, string str)
{

  for (int i = 0, len = str.length(); i < len; i++)
    {
        if (ispunct(str[i]))
        {
            str.erase(i--, 1);
            len = str.size();
        }
    }

   cout << str;
}
void removeSpaces(char *str)
{
  int count = 0;
    for (int i = 0; str[i]; i++)
    {
      if (str[i] != ' ')
      {
      str[count++] = str[i];
      }
    }

    str[count] = '\0';

}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void removePun(char len,string str);
void-removespace(char-str);
int main()
{
串句;
沙伦;
getline(cin,句子);
删除pun(len,句子);
移除空间(len);
返回0;
}
void removePun(char len,string str)
{
for(int i=0,len=str.length();icout原因是
cin
始终将空格(空格、制表符、新行…)视为输入值的终止,因此提取字符串意味着始终提取单个单词,而不是短语或整个句子

要从
cin
获取整行,存在一个名为
getline
的函数,该函数将流(cin)作为第一个参数,字符串变量作为第二个参数。例如:

string removeEvery(string str)
{
  int len = str.size();
  for (int i = 0; i < len; i++)
    {
        if (ispunct(str[i]))
        {
            str.erase(i--, 1);
            len = str.size();
        }
    }

   return str;
}

int main()
{
  string sentence;
  getline(cin, sentence);
  sentence = removeEvery(sentence);
  cout << sentence;
  return 0;
}
字符串删除(字符串str)
{
int len=str.size();
对于(int i=0;icout原因是
cin
始终将空格(空格、制表符、新行…)视为输入值的终止,因此提取字符串意味着始终提取单个单词,而不是短语或整个句子

要从
cin
获取整行,存在一个名为
getline
的函数,该函数将流(cin)作为第一个参数,字符串变量作为第二个参数。例如:

string removeEvery(string str)
{
  int len = str.size();
  for (int i = 0; i < len; i++)
    {
        if (ispunct(str[i]))
        {
            str.erase(i--, 1);
            len = str.size();
        }
    }

   return str;
}

int main()
{
  string sentence;
  getline(cin, sentence);
  sentence = removeEvery(sentence);
  cout << sentence;
  return 0;
}
字符串删除(字符串str)
{
int len=str.size();
对于(int i=0;icout句点删除代码没有问题。我认为它没有打印整个句子的原因是因为您使用了cin>>,它使用空格作为分隔符,只提取整个句子的一部分。您可以使用getline来解决这个问题

#include<string>
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>

bool compareSongs(std::string a, std::string b)
{
    return a < b;
}

void sortSongsAlphabetically(std::vector<std::string> &songs)
{
    std::sort(songs.begin(), songs.end(), compareSongs); 
}

std::string clean(std::string input)
{
    std::string new_string;
    std::stringstream ss{input};
    std::string word;

    /* parse sentence word by word to remove multiple spaces */
    while(ss >> word) {
       for (char c : word) {
            if (std::isalnum(c) && !std::isspace(c)) {
                new_string += c;
            } 
        }
        new_string += " ";
    }
    return new_string;
}

int main()
{
    std::vector<std::string> songs;
    std::string input;

    const int max_songs = 5;
    int songs_count = 0;

    while(getline(std::cin, input) && songs_count < max_songs) {
        std::string song = clean(input);
        songs.push_back(song); 
    }

    /* sort songs alphabetically */
    sortSongsAlphabetically(songs);

    std::cout << "Alphabetically sorted songs:" << std::endl;
    for (auto song : songs) {
        std::cout << song << std::endl;
    }

    return 0;

}
#包括
#包括
#包括
#包括
#包括

#include

您的句点删除代码没有问题。我认为它没有打印整个句子的原因是因为您使用的是cin>>,它使用空格作为分隔符,只提取整个句子的一部分。您可以使用getline来修复此问题

#include<string>
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>

bool compareSongs(std::string a, std::string b)
{
    return a < b;
}

void sortSongsAlphabetically(std::vector<std::string> &songs)
{
    std::sort(songs.begin(), songs.end(), compareSongs); 
}

std::string clean(std::string input)
{
    std::string new_string;
    std::stringstream ss{input};
    std::string word;

    /* parse sentence word by word to remove multiple spaces */
    while(ss >> word) {
       for (char c : word) {
            if (std::isalnum(c) && !std::isspace(c)) {
                new_string += c;
            } 
        }
        new_string += " ";
    }
    return new_string;
}

int main()
{
    std::vector<std::string> songs;
    std::string input;

    const int max_songs = 5;
    int songs_count = 0;

    while(getline(std::cin, input) && songs_count < max_songs) {
        std::string song = clean(input);
        songs.push_back(song); 
    }

    /* sort songs alphabetically */
    sortSongsAlphabetically(songs);

    std::cout << "Alphabetically sorted songs:" << std::endl;
    for (auto song : songs) {
        std::cout << song << std::endl;
    }

    return 0;

}
#包括
#包括
#包括
#包括
#包括

#include

如何删除空格?我如何做才能获得多个输入并更改每个输入?我已经更新了代码以获得多个输入并删除空格。最好是让
RemovePun
返回字符串。然后你可以用它做其他事情,这意味着函数只有一个职责。如果函数删除空格和标点符号,一个人应该更改名称。也许
clean
。你是对的,我不想做太多更改,并试图让它靠近OP的代码以便更好地理解,但你是绝对正确的,应该按照你的建议。删除一个空格怎么样?我如何才能做到这一点以获得多个inputs和它改变了每一个。我已经更新了代码以获得多个输入并删除空格。最好让
RemovePun
返回字符串。然后你可以用它做其他事情,这意味着函数只有一个职责。如果你让函数删除空格和标点符号,你应该更改名称。根据haps
clean
。你是对的,我不想做太多更改,并试图让它接近OP的代码以便更好地理解,但你是绝对正确的,应该按照你的建议。