A
arnuld
i have solved it. any suggestions for improving the code:
/* C++ Primer - 4/e
* chapter 3
* exercise 3.14
* STATEMENT
read some text into a vector,storing each word as an elelment in
the vector. transform each word into the uppercase letter. print the
transofrmed elelments from the vector as eight words per line.
*/
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::cout << "please enter some words seperated by newlines" <<
std::endl;
std::string a_word;
std::vector<std::string> svec;
while(std::cin >> a_word)
{
svec.push_back(a_word);
}
for(std::vector<std::string>::iterator iter=svec.begin(); iter !=
svec.end(); ++iter)
{
for(std::string::size_type ix=0; ix != (*iter).size(); ++ix)
{
((*iter)[ix]) = toupper((*iter)[ix]);
/* i call it the MESSY solution.
isn't there anything better ? */
}
}
int counter = 0;
for(std::vector<std::string>::const_iterator iter=svec.begin(); iter !=
svec.end(); ++iter)
{
if(counter == 7)
{
std::cout << *iter << std::endl;
counter = 0;
}
else
{
std::cout << *iter << " ";
++counter;
}
}
std::cout << std::endl;
return 0;
}
======= OUTPUT ===========
[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra ex_03-14.cpp
[arnuld@arch cpp ]% ./a.out
please enter some words seperated by newlines
aRnUld
Fraser
was fallING
out OFF his TraCKL from some MONTHS but NOW hE iS bAcK
ARNULD FRASER WAS FALLING OUT OFF HIS TRACKL
FROM SOME MONTHS BUT NOW HE IS BACK
[arnuld@arch cpp ]%
/* C++ Primer - 4/e
* chapter 3
* exercise 3.14
* STATEMENT
read some text into a vector,storing each word as an elelment in
the vector. transform each word into the uppercase letter. print the
transofrmed elelments from the vector as eight words per line.
*/
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::cout << "please enter some words seperated by newlines" <<
std::endl;
std::string a_word;
std::vector<std::string> svec;
while(std::cin >> a_word)
{
svec.push_back(a_word);
}
for(std::vector<std::string>::iterator iter=svec.begin(); iter !=
svec.end(); ++iter)
{
for(std::string::size_type ix=0; ix != (*iter).size(); ++ix)
{
((*iter)[ix]) = toupper((*iter)[ix]);
/* i call it the MESSY solution.
isn't there anything better ? */
}
}
int counter = 0;
for(std::vector<std::string>::const_iterator iter=svec.begin(); iter !=
svec.end(); ++iter)
{
if(counter == 7)
{
std::cout << *iter << std::endl;
counter = 0;
}
else
{
std::cout << *iter << " ";
++counter;
}
}
std::cout << std::endl;
return 0;
}
======= OUTPUT ===========
[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra ex_03-14.cpp
[arnuld@arch cpp ]% ./a.out
please enter some words seperated by newlines
aRnUld
Fraser
was fallING
out OFF his TraCKL from some MONTHS but NOW hE iS bAcK
ARNULD FRASER WAS FALLING OUT OFF HIS TRACKL
FROM SOME MONTHS BUT NOW HE IS BACK
[arnuld@arch cpp ]%