Pig Latin XD

I completed it :)

input text
filter
output piglatin

http://en.wikipedia.org/wiki/Pig_latin

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <cstdlib>
#include <iostream>
#include <conio.h>

using namespace std;

void cmd(char *choice){
     if(choice == "wait"){
        int c;
        printf( "\nPress a key to continue...\n" );
        c = getch();
        if (c == 0 || c == 224) getch();
     }
}

string killPunctuation(string str){
    string complete = "";
    char curChar;
    int i = 0;
    while(str[i]){ 
            curChar = str[i];
            if(isalpha(curChar) || isspace(curChar)){
                complete += curChar;
            }
            i++;
    }
    return complete;
}


string killCaps(string str){
    string complete = "";
    char curChar;
    int i = 0;
    while(str[i]){ 
            curChar = str[i];
            if(isupper(curChar)) curChar=tolower(curChar);
            i++;
            complete += curChar;
    }
    return complete;
}

int main(int argc, char *argv[]){
      
      /////////////////////////////////////
      //// output instructions for use ////
      /////////////////////////////////////
      /////////////////////////
      //// take user input ////
      /////////////////////////
      cout << "Type a message in english to converted to pig latin\n" << "Do not use capital letters or non-letters\n\n";
      string input;
      cout << "input > ";
      getline(cin,input);
      cout << endl;
      input = killPunctuation(killCaps(input));// filter
      /////////////////////////////////////////
      //// user input is stared in 'input' ////
      /////////////////////////////////////////
      ///////////////////////////////////////////////
      //// Split sentence into an array of words ////
      ///////////////////////////////////////////////
      char * cstr, *p; // cstr: string's size; p:  
      string str (input);
      cstr = new char [str.size()+1];
      strcpy (cstr, str.c_str());
      string words[40];
      int i = 0;
      p=strtok (cstr," ");
      while (p != NULL){
           words[i] = p;
           p=strtok(NULL," ");
           i++;
      }
      delete[] cstr; 
      int length = i;
      /////////////////////////////////////////////////////////
      //// done splitting sentence into an array of words  ////
      /////////////////////////////////////////////////////////
      /////////////////////////////////////////////////
      //// words have been stored in array 'words' ////
      /////////////////////////////////////////////////
      ///////////////////////////////////////////////////////
      //// length of the array is stored in 'length' - 1 ////
      ///////////////////////////////////////////////////////
      //////////////////////////// / /////////////
      //// next edit each word into pig latin ////
      /////////////////////////// /// ////////////
      string piglatin[40];
      string word;
      string mid;
      string Pig_Latin = "";
      
      i = 0;
      int a = 1;
      int wordLength;
      while(i != length){
          word = words[i];
          
          wordLength = word.length();
          
          while(a != word.length()){
               mid += word[a];
               a++;
          }
          word = mid + word[0] + "ay";
          piglatin[i] = word;
          Pig_Latin += piglatin[i] + " ";
          i++;
          mid = "";   
          a = 1; 
      }
      ////////////////////////////
      //// output the results ////
      ////////////////////////////
      ////////////////////////////////////////////////
      //// the Pig Latin is stored in 'Pig_Latin' ////
      ////////////////////////////////////////////////
      
      if(Pig_Latin == ""){
           cout << "No translation found!\nAll numbers and punctuation are filtered.\n";
      }else{
           cout << "Pig latin translation : " << Pig_Latin << endl;
      }
      
      cmd("wait");
      return EXIT_SUCCESS;
}


#include <conio.h>

:(

Don't bother making main take arguments if you aren't going to use them.

Ew, strcpy and memory allocation...just use an std::vector, then just .find() and .substr() to get the individual words...

if(Pig_Latin == ""){

You can replace that with: if(Pig_Latin.empty())

For your "KillCaps" you could use:
std::transform(str.begin(), str.end(), &tolower);

For your "Killpunct" you could use:
1
2
3
4
5
6
7
8
9
10
class add_char {
    std::string operator ()(std::string str, char chr) {
        if(isalpha(curChar) || isspace(curChar)) {
            str += chr;
        }
        return str;
    }
}

new_str = std::accumulate(str.begin(), str.end(), string(""), add_char);

^I think this works, haven't tested it though

Last edited on
Topic archived. No new replies allowed.