Pig Latin Help

edit: I guess long story short is how do you save and detect spaces in a string vector? Because the way I've been trying isn't working.

When I test output it looks empty with the vector item which would work except how do I detect an empty string vector element because in the case of double spaces I need my output string to have double spaces (no "ay" and right now that's what it gives)


Not homework, just practice I'm doing on another website but I'm very stuck on this. I'm soo close but it fails the random tests involving input with multiple space inputs.

Goal: Pig latin any input sentence (can be a word or sentence and can include non characters like ! ) So take the first letter and put it at the end with "ay" attached to it.

Like I said, it works on everything except the random tests involving double spaces. From what I can tell when I fill my vector it sometimes saves and sometimes doesn't save the space I'm trying to detect in:

1
2
3
4
5
6
7
for (int i = 0; i < str.length(); i++ ) { 
    if (str[i] == ' ') { 
      if (str[i+1] == ' ') {
       words.push_back(" "); 
       i++;
       word = ""; 
      }


Then when I try and detect it, it doesn't detect. I've tried lots of ways of detecting spaces including isspace

1
2
3
4
5
6
7
8
9
10
11
// last word == 1
     } else if ( count == words.size()-1 && w.length() == 1 ) {
      if ( w == "" || w == " ") {
        pigged += " ";
        cout << "appended spaces#2" << endl;
        break;
      } else if ( !(isalpha(w[0])) ) {
       pigged += w;
       break;
      }



the full code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string pig_it(string str)
{
  string ay = "ay";
  vector<string> words;
  string word = ""; 
  for (int i = 0; i < str.length(); i++ ) { 
    if (str[i] == ' ') { 
      if (str[i+1] == ' ') {
       words.push_back(" "); 
       i++;
       word = ""; 
      }
      else {
        words.push_back(word); 
        word = ""; 
      }
    } 
    else{ 
       word = word + str[i]; 
    } 
  }  
  words.push_back(word); 
  
  string pigged;
  char temp;
  int count = 0;
  //cout << "word start";
 // for (auto w : words) {
 //   cout << w << " ";
  //}
  //cout << "end" << endl;
  
  for (auto w : words) {
     // testing
     cout << "start" << w << "end" << endl;

     // last word larger than 1
     if (count == words.size()-1 && w.length() > 1) {
      temp = w[0];
      w = w.erase(0,1);
      pigged += w + temp + "ay";
      break;
        
     // last word == 1
     } else if ( count == words.size()-1 && w.length() == 1 ) {
      if ( w == "" || w == " ") {
        pigged += " ";
        cout << "appended spaces#2" << endl;
        break;
      } else if ( !(isalpha(w[0])) ) {
       pigged += w;
       break;
      }
      else {
       pigged += w + "ay";
       break;
      }
      
    // if length is just 1 but not last word
    } else if ( w.length() == 1 && count != words.size()-1) {
      pigged += w + "ay ";
      count++;
    
    // if not the last word but is a space
    } else if (w[0] == ' ' ) {
      pigged.append(" ");
      cout << "appended spaces#1" << endl;
      count++;
      
    // if it's not the last word 
    } else if ( count != words.size() ) {
      temp = w[0];
      w = w.erase(0,1);
      pigged += w + temp + "ay ";
      count++;
      } 
    }
    //pigged.pop_back();
      //cout << "start" << pigged << "end" << endl;
      
  return pigged;
}
Last edited on
got it. I fixed the vector a bit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 0; i < str.length(); i++ ) { 
    if (str[i] == ' ') { 
        if (word != "") {
          words.push_back(word); 
          word = ""; 
        } else {
          words.push_back(" ");
        }
    } 
    else { 
       word = word + str[i]; 
    } 
  }  
  words.push_back(word); 



and moved the test for space to the top. had to finagle it a bit because of weird spaces appearing at the end

1
2
3
4
// if is a space
     if ( w == " " || w[0] == NULL && count != words.size()) {
      pigged += " ";
      count++;
Topic archived. No new replies allowed.