1

I'm working on right now with string in vector . And i got my self in dead end . I've manipulate with vector<int> elements, and understand how to work with them ! I know how to work with string ! But i can't get thru the part where i need to change string element value in vector .I mean i don't know what to do in loop with "do something". So to be short i give the task on witch i work right now.

Read a sequence of words from cin and store values in vector.After you've read all words,process the vector and change each word to uppercase

Here what I've got so far

int main ()  
{
    vector<string> words;    //Container for all input word
    string inp;              //inp variable will process all input 

    while (cin>>inp)         //read
       words.push_back(inp); //Storing words 

    //Processing vector to make all word Uppercase
    for (int i = 0; i <words.size(); ++i)
     //do something

         words[i]=toupper(i);
    for (auto &e : words)    //for each element in vector 
     //do something

     cout<<e;

    keep_window_open("~");
    return 0;
}  

This first for statement is not right i try access vector elements and change words to upper but it did't work for me its just sample
I've try many ways to access the vector element but when trying to use string member functions toupper() on vector i'm getting messy with code and logical mistakes!
Thanks For your time . Sorry for mistakes i made in spelling words

AlexGreat
  • 187
  • 1
  • 4
  • 14
  • 1
    `toupper` only works on single characters. You might try combining it with `std::transform`. – chris Jun 08 '13 at 23:43
  • `toupper` work with whole `string` in rangebase `for` statement. – AlexGreat Jun 08 '13 at 23:45
  • 1
    Applying `toupper` to the *index* surely won't give anything useful. – celtschk Jun 08 '13 at 23:45
  • possible duplicate of [Convert a String In C++ To Upper Case](http://stackoverflow.com/questions/735204/convert-a-string-in-c-to-upper-case) – Stuart Golodetz Jun 08 '13 at 23:46
  • @StuartGolodetz Didnt find anything usefull out there ! – AlexGreat Jun 08 '13 at 23:50
  • @AlexGreat: The link was based on the thought that your underlying problem seems to be how to convert elements of the vector (strings) to uppercase. Put another way, `words[i]` is a string, and you can do anything to it that you would do to any other string. If you know how to make a string uppercase, you can make `words[i]` uppercase. – Stuart Golodetz Jun 08 '13 at 23:54

3 Answers3

4

Try this:

for (auto& word: words)
  for (auto& letter: word)
    letter = std::toupper(letter);
celtschk
  • 19,311
  • 3
  • 39
  • 64
2

This can be fixed by using the std::transform Standard algorithm to iterate through the characters of the words. You can also use std::for_each instead of the manual loop.

#include <string>
#include <algorithm>
#include <iostream>
#include <cctype>
#include <vector>

int main()  
{
    std::vector<std::string> words;
    std::string inp;

    while (std::cin >> inp)
       words.push_back(inp);

    std::for_each(words.begin(), words.end(), [] (std::string& word)
    {
        std::transform(
            word.begin(),
            word.end(), 
            word.begin(), (int (&)(int)) std::toupper
        );
    })

    for (auto &e : words)
        std::cout << e << std::endl;
}

And here is a demo.

David G
  • 94,763
  • 41
  • 167
  • 253
0

You can do this in the first for loop:

string w = words.at(i);
std::transform(w.begin(), w.end(), w.begin(), ::toupper);
jh314
  • 27,144
  • 16
  • 62
  • 82