-1

In my program I have a text file that is read into an array that tokenizes each word. I need it this way so that I can compare the words to the words found in my Binary Tree. Issue is... some duplicates of words are not formatted the same way (one is uppercase and one is lowercase) and I need them to be so they can be found in my Binary Tree.

So my question is: How do I change my whole array to lowercase?

Here is what I tried so far:

#include <iostream>
#include "Binary_SearchTree.h"
#include "Node.h"
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

const int SIZE = 100;
string myArray[SIZE];

int main() {

    // first constructor will be used since it is empty
    Binary_SearchTree<string> *tree = new Binary_SearchTree<string>();

    string token, lines;
    ifstream file("hashtags.txt");

    while (getline(file, lines)){
            tree -> insertNode(lines);

    }

    // Convert all strings in myArray to all-lower
    myArray = tolower(myArray);

    // tokenize tweet into an array to search
    ifstream tweet1("exampleTweet.txt");
    if(tweet1.is_open())
    {

    while (getline(tweet1, token)){
            for(int i = 0; i < SIZE; ++i)
            {
            tweet1 >> myArray[i];
            }

    }
    tweet1.close();

}
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
narue1992
  • 1,143
  • 1
  • 14
  • 40
  • 1
    I removed your superfluous flood of error-messages since their reasons are already explained below. Please read the answer and the comments more carefully. – Baum mit Augen Apr 09 '15 at 22:01

1 Answers1

3

With C++11 and later, you can downcase an array of strings like this:

#include <algorithm>
#include <cctype>
#include <string>

std::string myArray[23];

// ...

for (std::string & s : myArray)
    std::transform(s.begin(), s.end(), s.begin(),
                   [](unsigned char c) { return std::tolower(c); });

Alternatively:

for (std::string & s : myArray)
    std::for_each(s.begin(), s.end(), [](char & c) {
        c = std::tolower(static_cast<unsigned char>(c)); });

Or even:

for (std::string & s : myArray)
    for (char & c : s)
        c = std::tolower(static_cast<unsigned char>(c));

If you only have C++98 support, use the following loops:

for (std::size_t i = 0; i != 23; ++i)
{
    std::string & s = myArray[i];
    for (std::string::iterator it = s.begin(), e = s.end(); it != e; ++it)
    {
        *it = std::tolower(static_cast<unsigned char>(*it));
    }
}

You get the idea.

Don't forget to convert the character to unsigned char, since that's what std::tolower expects. (See this question for a discussion.) Many C I/O functions are expressed in terms of unsigned char-converted-to-int, since usually an int is big enough to represent all values of an unsigned char plus additional out-of-band information, and char and unsigned char are roundtrip convertible both ways as well as layout-compatible.

Community
  • 1
  • 1
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    @narue1992 The last version (updated!) should work for all compilers. For the rest, you need to enable C++11 support. That is not possible for all compilers, for gcc and clang you can do it with `-std=C++11` (or `-std=C++0x` if your compiler is semi-ancient). – Baum mit Augen Apr 09 '15 at 21:36
  • 1
    @narue1992 I think this vague discussion in the comments is long enough now. Kerrek's solutions are correct, if you cannot get them to work ask a new question including an [SSCCE](http://www.sscce.org) so people can actually see what you are doing wrong. – Baum mit Augen Apr 09 '15 at 22:08