I was using std::string. The question in the excercise was:
"Read some text into a vector, storing each word in the input as an element in the vector. transform each word into uppercase letters. Print the transformed elements from the vector, printing eight words to a line."
I done it, but I think it might be a little messy.. lol.
Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::string> vText; //Container for each word
std::string sText; //String of words
getline(std::cin, sText); //Get input
int newPos = 0;
int wordCount = 0;
std::string temp;
while(sText.find(' ', newPos) <= sText.size())
{
wordCount++;
newPos = sText.find(' ', newPos) + 1;
}
newPos = 0;
wordCount++;
for(int i = 0; i < wordCount; i++)
{
int j = sText.find(' ', newPos);
if(newPos > 0)
{
temp = sText.substr(newPos, j - newPos);
}
else
{
temp = sText.substr(newPos, j);
}
std::transform(temp.begin(), temp.end(), temp.begin(), toupper);
vText.push_back(temp);
newPos = j + 1;
}
wordCount = 0;
for(std::vector<std::string>::size_type i = 0; i < vText.size(); i++) //Output each word 8 at a time
{
if(wordCount <= 8)
{
std::cout << vText[i] << " ";
wordCount++;
}
else
{
std::cout << "\n" << vText[i] << " ";
wordCount = 0;
}
}
std::cout << std::endl;
return 0;
}
Be gentle... very gentle.
EDIT: Forgot to put them in upper case... done