user12230466
user12230466

Reputation: 27

Function to remove periods and spaces

I need to create a function that will remove all periods from a sentence and the spaces

It is only printing out the first word of the sentence not the whole sentence. If I put "hello. my name is" it will only print "hello" but remove the period. I also need to remove the spaces here is my code so far

#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';

}

Upvotes: 0

Views: 481

Answers (2)

Yasir Khan
Yasir Khan

Reputation: 693

There's nothing wrong with your period removal code. I think the reason it's not printing the whole sentence is because you are using cin >> which uses whitespace as delimiter and extracts only a part of the whole sentence. You can fix this by using 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;

}

You can see it live

Upvotes: 0

Javier Silva Ort&#237;z
Javier Silva Ort&#237;z

Reputation: 2982

The reason is that cin always considers spaces (whitespaces, tabs, new-line...) as terminating the value being input, and thus extracting a string means to always extract a single word, not a phrase or an entire sentence.

To get an entire line from cin, there exists a function, called getline, that takes the stream (cin) as first argument, and the string variable as second. For example:

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

Note that removeEvery now returns a string because as you had it the change is local to that function. If you meant for the cleaned string to be local to removeEvery then you can leave it as you had it, just recall the getline function. There's a lot more info on input/output handling here in the official reference.

Upvotes: 2

Related Questions