How to ignore punctuation in piglatin program (edited)

I figure out how to get it to change the sentence into piglatin, but after hours of searching on how to ignore punctuation, I am still confused on how to do this. I want the punctuation to be at the end of any word that has any punctuation. How should I do this?


Given the following:

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
struct Word
{
	string english;
	string piglatin;
};


Word * piglatinCon(Word sentenceArr [], int &wordNum)
{
	string s, c;
	int i = 0;
	Word *str = nullptr;
	str = new Word[wordNum];

	for (int count = 0; count < wordNum; count++)
	{
		if (*sentenceArr[count].english.begin() != 'e'  &&            *sentenceArr[count].english.begin() != 'i' && *sentenceArr[count].english.begin() != 'o' && *sentenceArr[count].english.begin() != 'u' && *sentenceArr[count].english.begin() != 'a' && *sentenceArr[count].english.begin() != 'E' &&   *sentenceArr[count].english.begin() != 'I' && *sentenceArr[count].english.begin() != 'O' && *sentenceArr[count].english.begin() != 'A' && *sentenceArr[count].english.begin() != 'U')
		{
			// Converts to pig latin
			c = *sentenceArr[count].english.begin();
			s = sentenceArr[count].english.erase(0, 1);
			str[i].piglatin = s + c + "ay";
		}

		// Any other letter it will just add way to the end of the word
		else
			str[i].piglatin = sentenceArr[count].english + "way";
		i++;
	}

	return str;
}
Last edited on
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
#include <iostream>
#include <string>
#include <regex>
#include <cctype>//ispunct
#include <algorithm>

void removePunctStdLib(std::string s)
{
      s.erase (std::remove_if(s.begin (), s.end (), ispunct), s.end ());
      std::cout << s << "\n";
}
//http://en.cppreference.com/w/cpp/algorithm/remove
//https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
void removePunctRegex(std::string s)
{
    std::regex re("[[:punct:]]");
    s = std::regex_replace(s, re, "");
    std::cout << s << " \n";
}
//start with regex here:http://www.cplusplus.com/reference/regex/ECMAScript/

int main()
{
    removePunctStdLib(std::string("Hel!l,o? W;or.ld\n"));
    removePunctRegex(std::string("Hel!l,o? W;or.ld\n"));
}

Thanks for your help, I was able to remove the punctuation and then do the piglatin conversion. So now I want to add the punctuation after the conversion, so is there any way to do this?

My implementation of the Code:
1
2
3
4
5
6
 else
		{
			 temp[i].english += sentence[count];
			 regex re("[[:punct:]]");
			 temp[i].english  = regex_replace( temp[i].english, re, "");
		 }
Last edited on
So now I want to add the punctuation after the conversion

add the punctuation back to what, temp[i].english? In which case it'd be the temp[i].english before the regex_replace
So I would add the puncuation back to the piglatin field after the conversion.
why not just save a copy of the original string with the punctuation and use another copy to remove punct?
Topic archived. No new replies allowed.