input manipulation

Hello,

I am having trouble converting a text file into pig latin. I managed to store the file into a vector of strings, and attach the word "way" to the end of each word which has a vowel in beginning, but words like "sky..." adds the way like this: "sky...way". Is there a way to store, omitting the punctuations?



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

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

void getFile(vector<string>&);
void identify(vector<string>&);
int main()
{
	vector<string> list;
	getFile(list);
	cout << endl;
	identify(list);
	cout << endl;
	system("pause");
}
void getFile(vector<string>& pbr)
{
	
	string word;
	ifstream input("input.txt");
	while(input >> word)
	{
		pbr.push_back(word);
	}

	//test output
	for(unsigned int n = 0; n < pbr.size(); n++)
		cout << pbr.at(n) << " ";

	

}

void identify(vector<string>& pbr)
{


	string poop = "way";

	
	for(unsigned int x = 0; x < pbr.size(); x++)
	{
		string store = pbr[x];

		//converts the first letter of the word to lower case	
		store[0] = tolower(store[0]);
		
		//create an array of chars to hold the vowels
		
		char vowels[5] = {'a', 'e', 'i', 'o', 'u'};
		for(int n = 0; n < 5; n++) 
		{
			if(store[0] == vowels[n])
			{
				store.replace(store.length(),store.length(),poop);		
				break;
			}
		}

		pbr[x] = store;
	}
		//test output
		for(unsigned int n = 0; n < pbr.size(); n++)
		cout << pbr.at(n) << " ";





}
store.replace(store.length(),store.length(),poop);

You could have just used append there... But I guess it has nothing to do with your question.

I do not know if it's a good way to do it, but you can try adding another loop inside your nested loop. You would use it to walk through the string and searching for the end of the letters.

1
2
3
std::string::size_type i;
for (i = store.length() - 1; !isalpha(store[i]) && i >= 0; i--);
store.insert(i + 1, poop);
hey fg109 thanks for the code, it worked, but I decided to forgo the vectors, just input into a string var and manipulate with string class. I managed to get the code to look exactly what i needed (several hrs later :/ ).

though its not required, There is just one thing i am fussing over, words that have a dash between them like "Double-barrelled". words like "one...way" i manged to put the ... last using a swap, but for dashed words, how do i keep the dash in between instead of last?
Last edited on
Topic archived. No new replies allowed.