1

People who learned how to type before word processors often add two spaces after a period ending a sentence. Write a function singleSpaces that accepts a string and returns that string with all occurrences of two spaces after a "." into changed single spaces.)

This is what I have; what am I doing wrong?

#include <cmath>
#include <iostream>
using namespace std;



string forceSingleSpaces1 (string s) {
    string r = "";
    int i = 0;
    while (i < static_cast <int> (s.length()))  {
        if (s.at(i) != ' ')  {
            r = r + s.at(i);
            i++;
        } else  {
            r +=  ' ';
            while (i < static_cast <int> (s.length()) && s.at(i) == ' ')
                i++;
        }
    }
    return r;
}
bluish
  • 26,356
  • 27
  • 122
  • 180
Kc North
  • 31
  • 1
  • 10
  • 2
    `People who learned how to type before word processors often add two spaces after a period ending a sentence` Not pertinent to your question, but I was taught that was a style used in British English. – Jesse Good Apr 22 '12 at 23:16
  • Writing it as two loops can make it harder to think about. It's allowed, but unusual. Try doing it in just one "while{}" loop, but keeping track of what happened recently... At least that's one way you might approach it. (Though, just reading , it looks ok to me. :-) – david van brink Apr 22 '12 at 23:17
  • 3
    When asking what you've done wrong, it is _immensely_ helpful if you include four or five short test inputs and their corresponding outputs as well as the output you _expected_ to get. – sarnold Apr 22 '12 at 23:26
  • You may also wish to investigate the use of [**`std::remove_copy_if()`**](http://www.cplusplus.com/reference/algorithm/remove_copy_if/) to solve this problem, this would save you rolling your own loops but may be outside the scope of your homework. Also, you should pass `std::string`s around *by reference* (`&`) to prevent needless copying. – johnsyweb Apr 23 '12 at 01:38
  • @Johnsyweb To use `remove_copy_if` you'd have to create a functor which kept track of the previous two characters. – Peter Wood Apr 23 '12 at 07:59
  • @PeterWood: Alternatively, you could just write a function with a static variable or two! I mentioned the use of `std::remove_copy_if()` merely to pique the interest of the OP into the algorithms which C++'s standard library provides; not to suggest that it was used in a homework answer (otherwise I would have provided it as an answer). TMTOWTDI and all that. – johnsyweb Apr 23 '12 at 10:54

3 Answers3

2

In your assignment there is talk about double spaces after dot, and not all double spaces in text. So you should modify your code so that it

  • waits for a '.'and not ' ',
  • when '.' is intercepted then add it, after that add any single space

you can think of this code as two states machine:

state 1 - is when you are looping on any non '.' character, in this state your code adds to result all what it finds

state 2 - is when '.' is found, and in this state you use different code, you add '.' to results and ater that exactly single space (if any one was found)

this way you have your problem divided into two sub problems

[edit] - replaced source code with modification hints

marcinj
  • 48,511
  • 9
  • 79
  • 100
2

You might (re)use a more general function that replaces occurrences of a given string within a string with another string, as described here.

#include <string>
#include <iostream>

void replace_all(std::string& str, const std::string& from, const std::string& to) {
    size_t start_pos = 0;
    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
    }
}

int main() {
    std::string text = "I'm old.  And I use two spaces.  After periods.";
    std::string newstyle_text(text);
    replace_all(newstyle_text, ".  ", ". ");
    std::cout << newstyle_text << "\n";

    return 0;
}

Update

If you are not afraid of being on the cutting edge, you might consider using TR1 regular expressions. Something like this should work:

#include <string>
#include <regex>
#include <iostream>

int main() {
    std::string text = "I'm old.  And I use two spaces.  After periods.";
    std::regex regex = ".  ";
    std::string replacement = ". ";
    std::string newstyle_text = std::regex_replace(text, regex, repacement);

    std::cout << newstyle_text << "\n";

    return 0;
}
Community
  • 1
  • 1
epidemian
  • 18,817
  • 3
  • 62
  • 71
0
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

//1. loop through the string looking for ".  "
//2. when ".  " is found, delete one of the spaces
//3. Repeat process until ".  " is not found.  

string forceSingleSpaces1 (string str) {
    size_t found(str.find(".  "));
    while (found !=string::npos){
        str.erase(found+1,1);
        found = str.find(".  ");
    }

    return str;
}

int main(){

    cout << forceSingleSpaces1("sentence1.  sentence2.  end.  ") << endl;

    return EXIT_SUCCESS;
}
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • I have a single loop one I did that is for all spaces how do I post it to show code it wont let me post an answer to my own question so I can get the window that allows code – Kc North Apr 23 '12 at 01:30
  • go to http://ideone.com/, you can add code with sample data input there, and post link here. – marcinj Apr 23 '12 at 13:13