Upper case to lower case strings

I need help writing a function that turns a string into all lower case characters. I am using an iterator to traverse the string and I don't know how to complete/ fix this.
// filename: WordCountApp.cpp

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;

#include "word.h"
#include "bst2.h"

void lowerCase(string &s);

int main()
{
  ifstream fin;
  ofstream fout;

  fin.open("review.txt");
  if (!fin) 
  {
    cerr << "Input file opening error\n";
    exit(1);
  }

  fout.open("words.txt");
  if (!fout) 
  {
    cerr << "Output file opening error\n";
    exit(1);
  }

  BST<Word> bst;
  int total = 0;
  string s;

  while (fin >> s) 
  {
	  lowerCase(s);
	  total++;

	  Word *ptr;
	  ptr = bst.find(s);
	  if(*ptr != s)
		  bst.insert(s);
  }

  cout << "Total number of words read:   "
       << total << endl;
  cout << "Total number of unique words: "
       << bst.size() << endl;
  cout << endl;

  Word maxWord = bst.findMax();
  cout << "** FindMax returns \"" << maxWord.wstring << "\", "
                                  << maxWord.count << endl << endl;
  bst.printTree(fout);

  fin.close();
  fout.close();

  system("pause");
  return 0;
}

void lowerCase(string &s)
{
	string::iterator t = s.begin();

	while(*t != NULL)
	{
		if(isalpha(*t))
		{
			if(isupper(*t))
				tolower(*t);
		}
		t++;
	}
}

Open in new window

villmundAsked:
Who is Participating?
 
phoffricConnect With a Mentor Commented:
The tolower function has prototype:
     int tolower ( int c );
"Converts parameter c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent. If no such conversion is possible, the value returned is c unchanged."
        http://www.cplusplus.com/reference/clibrary/cctype/tolower/

From this description, you can see that the tests isalpha and isupper, while clear, are not necessary.

Notice that the input parameter c is passed by value. So, even if you thought that tolower() changes c in the function itself, inside the function, the c would be just a local variable on the tolower's call frame on the stack. That is, the lowercase value of c would not be known to the caller. The converted value is obtained by the return value.

Return Value
The lowercase equivalent to c, if such value exists, or c (unchanged) otherwise. The value is returned as an int value that can be implicitly casted to char.
0
 
jkrConnect With a Mentor Commented:
Since you are using an iterator, the test

while(*t != NULL)

is the problem, that should be

while(t != s.end())

e.g.

void lowerCase(string &s)
{
	string::iterator t = s.begin();

	while(t != s.end())
	{
		if(isalpha(*t))
		{
			if(isupper(*t))
				tolower(*t);
		}
		t++;
	}
}

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
jhshuklaConnect With a Mentor Commented:
phoffric did much of the explanation. in addition to jkr's loop fix, you need this:
*t = tolower(*t);

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
No need to reinvent the wheel, just use std::transform.
http://www.cplusplus.com/reference/algorithm/transform/
#include <algorithm>
#include <string>
#include <iostream>

int main()
{
   std::string s("ALL UPPER TO ALL LOWER USING TRANSFORM");
   std::transform(s.begin(), s.end(), s.begin(), ::tolower);
   std::cout << s << std::endl;
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
Or, if you need to take locate into consideration
http://www.cplusplus.com/reference/std/locale/locale/
http://www.cplusplus.com/reference/std/functional/bind2nd/
http://www.cplusplus.com/reference/std/locale/tolower/
#include <algorithm>
#include <string>
#include <locale>
#include <iostream>
#include <functional>

struct to_lower : std::binary_function<char, std::locale, char>
{
   char operator()(char const c, std::locale const & l) const { return std::tolower(c, l); }
};

int main()
{
   std::string s("ALL UPPER TO ALL LOWER USING TRANSFORM");
   std::transform(s.begin(), s.end(), s.begin(), std::bind2nd(to_lower(), std::locale()));
   std::cout << s << std::endl;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.