Operation with one word at a time?

Hey guys, I recently started learning C++. I'm having troubles with a certain problem which converts the words in a sentecnce in a certain way according to the user's input. Basically if the user inputs an integer n=2 and the sentence: "Potatoes, are, cool!" it should read "esPotato, rea, olco!". I wrote a pretty basic function which transforms 1 given word of type string into an char array and then operates with the array to make the necessary adjustments of the letters.

However, I'm having troubles sending exactly one word to my function.

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
 
   int n=0;
    int z=0, q=0;
    int m=0;
    string word;
    string exp;
    string gmo;
    getline(cin, word);
    char temp[word.size()];
    cin>>n;
    strcpy(temp, word.c_str());
     for (int i=0; i<=word.size()-1;i++)
    {
        if (temp[i]=',')
        {
            for (q=m;q<=i-1;q++)
            {
                exp+=temp[q];
            }
            gmo+=New(exp, n)+",";
            exp.clear();
            m=i+1;
        }
        if (temp[i]=' ')
        {
            for (q=m;q<=i-1;q++)
            {
                exp+=temp[q];
            }
            gmo+=New(exp, n)+" ";
            exp.clear();
            m=i+1;
        }
        if (temp[i]='.')
        {
            for (q=m;q<=i-1;q++)
            {
                exp+=temp[q];
            }
            gmo+=New(exp, n)+".";
            exp.clear();
            m=i+1;
        }
        if (temp[i]='\n')
        {
            for (q=m;q<=i-1;q++)
            {
                exp+=temp[q];
            }
            gmo+=New(exp, n);
            exp.clear();
            m=i+1;
        }


    }


    cout<<gmo;

Where New is the function which manipulates the words.
What I do here is read a whole line from the input and convert it into char array, then check for every single letter if it is equal to "," " " "." "\n". (I don't even know if the last one is a thing at all), and if it isnt then it is copied to
the string exp, then converted by New, and then stored in gmo. However, the code doesnt seem to work, and I'm not suprised given how clunky it is.
My question is is there an easier way in which a person can inupt a whole line with punctuation, and to be able to send every word into my New function directly?
to send every word into my New function directly?
You may use stringstream:

http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream

int main () {


    string line;
    getline(cin, line);

  std::stringstream ss{line};

    string word;
    while(ss >> word)
    {
        process_word(word);
    }

  return 0;
}


convert it into char array
This is completely unnecessary, std::string provides everything you need including [] and find functions.
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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>//std::istringstream
#include <utility>//std::move

using vecString = std::vector<std::string>;//typedef C++11 style to reduce typing

vecString& newString (vecString& v);

int main()
{
    std::cout << "Enter input: \n";
    std::string input;
    vecString myStrings;
    getline(std::cin, input);
    std::istringstream stream(input);//create istringstream object, stream, with input
    while (stream)//check stream
    {
        std::string temp{};
        getline (stream, temp, ',');//split string by the comma delimiter
        myStrings.push_back(std::move(temp));
    }
    newString(myStrings);
}

vecString& newString (vecString& v)
{
    std::cout << "Enter key \n";
    unsigned int key{};
    std::cin >> key;
    for (auto& elem : v)
    {
        if (key < elem.size())//check the key vis-a-vis length of the word
        {
            std::string temp = elem.substr(elem.size()-key);//peel of the last k words
            elem = temp + elem.substr(0, elem.size()-key);//stick the last k words to the front substring
        }
    }
    return v;
}
Last edited on
Hey guys, thanks for the quick responses. coder 777 can you elaborate a bit more on how to use stringstream? What benefits it has and generally what is it? As I said I'm quite new to c++.
gunnerfunner I'm sorry man but your code is way to complicated for me.. I have no idea what most of it means.
Topic archived. No new replies allowed.