C++ write -for loop- more professional

Hi guys, could you please check this code below and suggest more professional solution, but not tooo professional, let it stay on beginners level. Thank you in advance.

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
// erasing specific words from sentence,
//   except if it is the first word
//     in that sentence
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
string sSentence;
cout << "Input a sentence: ";
getline(cin, sSentence);	
// input this: na alfa na beta ne gamma ne delta ni epsilon; output: NABGDE

for(size_t i = 0; i < sSentence.length(); i++)
    sSentence[i] = tolower(sSentence[i]);
char del[]=" ";
//
vector<string>skipThis;
skipThis.push_back("na");
skipThis.push_back("ne");
skipThis.push_back("ni");
//
char sentence[sSentence.length()+1];
strcpy(sentence, sSentence.c_str());
char *pch;
pch = strtok(sentence, del);
vector <string> words;
while(pch != NULL)
    	{
    	words.push_back(pch);
	pch = strtok(NULL, del);
	}

// ************************************************************************
// How to improve this part? do while, while, some vector function...?
// -------------------------

// if /na, ne, ni/ == the first word, do not check, should stay in sentence

size_t k = words.size();
for (size_t j = 0; j < k; j++)
{
    for (size_t i = 0; i < skipThis.size(); i++)
    {
// do not check first word
    vector<string>::iterator it = find(words.begin()+1, words.end(), skipThis.at(i));     
        if (it != words.end())
        {
            int d = distance(words.begin(),it);
            words.erase(words.begin()+d);
            k--;
        }
    }
}

//********************************************************************

vector<char>fl;
for(size_t i = 0; i < words.size(); i++)
{
    char p = toupper(words.at(i).at(0));
    fl.push_back(p);
}
//
cout << '\n';
for(size_t i = 0; i < fl.size(); i++)
     cout << fl.at(i);
return 0;
}
Last edited on
What about the erase-remove idiom?

Wow, sound like very elegant solution, I'll try it. Thank you.
----
It works perfect!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// ******************************************************************
// Improved part
// -------------------

// if /na, ne, ni/ == the first word, do not check, should stay in sentence

    for (size_t i = 0; i < skipThis.size(); i++)
    {
                                                // do not check first word
    vector<string>::iterator it = find(words.begin()+1, words.end(), skipThis.at(i));     
     if (it != words.end())
                                               // do not remove first word
             words.erase(remove(words.begin()+1, words.end(), skipThis.at(i)), words.end() ); 
    }
// input: na na alpha beta
//output: NAB


// ********************************************************************* 
Last edited on
Topic archived. No new replies allowed.