How to change elements in a vector to lower case

Aug 26, 2008 at 2:03am
Hello,
I am wondering how do you apply tolower() to elements in a string vector. Below is the code that I have so far and it isn't working!!

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <ctype.h>


using namespace std;
int main() {
vector<string> vec;
vec.push_back("SEE"); vec.push_back("BE"); vec.push_back("Can");
//sort(vec.begin(), vec.end());
for (unsigned int i = 0; i < vec.size(); i++)
{
vec[i]=tolower(vec[i]);
cout << vec[i] << " ";
}
return 0;
}
At this time I would like to know how would you go about doing this? Any help will be greatly appreciated!!
Aug 26, 2008 at 2:04am
Here is mine:

1
2
3
4
5
6
7
string CBaseConfigLoader::upperToLower(string Input) {
    for (int i = 0;i < (int)strlen(Input.c_str()); ++i)
       if (Input[i] >= 0x41 && Input[i] <= 0x5A)
        Input[i] = Input[i] + 0x20;

    return Input;
}
Last edited on Aug 26, 2008 at 2:04am
Aug 26, 2008 at 2:26am
You can't use tolower() on an entire string. You'll need to map it on. For example:
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
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const char* _strings[] =
  {
  "SEE",
  "BE",
  "Can"
  };

int main()
  {
  vector <string> vec( _strings, _strings +3 );

  // for each string in the vector
  for (unsigned n = 0; n < vec.size(); n++)
    {
    // convert the string to lowercase
    transform(
      vec[ n ].begin(),
      vec[ n ].end(),
      vec[ n ].begin(),
      ptr_fun <int,int> ( &tolower )
      );
    // show the user the result
    cout << vec[ n ] << endl;
    }
    
  return 0;
  }

Have fun!
Aug 28, 2008 at 6:27am
Yes, the problem is that the tolower() function is used to convert one character to lowercase. Another solution is that you write the function to entire strings:
1
2
3
4
5
6
7
8
string toLowerCase(string s)
{
    string r = s;
    for (int i = 0; i < r.size(); i++)
        if (r[i] >= 'A' && r[i] <= 'Z')
            r[i] = tolower(r[i]);
    return r;
}


Now you can use your code, just change tolower to toLowerCase:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <ctype.h>


using namespace std;
int main() {
vector<string> vec;
vec.push_back("SEE"); vec.push_back("BE"); vec.push_back("Can");
//sort(vec.begin(), vec.end());
for (unsigned int i = 0; i < vec.size(); i++)
{
vec[i]=toLowerCase(vec[i]);
cout << vec[i] << " ";
}
return 0;
}
Aug 28, 2008 at 7:34pm
Errr... My reply does just that =\
Aug 29, 2008 at 12:58pm
Yes, it does. :) I just don't understand why you need the CBaseConfigLoader:: namespace or whatsoever.
Last edited on Aug 29, 2008 at 12:58pm
Aug 29, 2008 at 1:24pm
I don't understand why everyone wants to write big old functions to do what the transform() algorithm will do for you in a single line of code.
:-|
Aug 31, 2008 at 7:31pm
@HarryGabriel91: CBaseConfigLoader is one of my classes in a project, As I said it's a copy + paste of mine. So you don't need it. But I'd expect people to be able to pick up on that.
Topic archived. No new replies allowed.