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?