0

I am having trouble getting started with a program. I need to read in each word from a file, then convert it to lower case. I would like to std::cout each word after I find it. I assume I need to use Cstr() some how. I am guessing I should use something like

ofs.open(infile.c_str());

but how to lower case?

string[i] = tolower(string[i]);

then,

std::cout << string[i];

Thanks for the help.

mfontanini
  • 21,410
  • 4
  • 65
  • 73
user1128637
  • 227
  • 3
  • 5
  • 13

4 Answers4

2

Here is a complete solution:

#include <ctype.h>
#include <iterator>
#include <algorithm>
#include <fstream>
#include <iostream>

char my_tolower(unsigned char c)
{
    return tolower(c);
}

int main(int ac, char* av[]) {
    std::transform(std::istreambuf_iterator<char>(
        ac == 1? std::cin.rdbuf(): std::ifstream(av[1]).rdbuf()),
        std::istreambuf_iterator<char>(),
        std::ostreambuf_iterator<char>(std::cout), &my_tolower);
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • You probably want cctype instead of ctype.h – ipc Mar 18 '12 at 22:00
  • @ipc: Do I? Why would I? Just so `tolower()` is put into namespace `std`? The above code compiles and executes as expected. I could have included `` and used `std::tolower(c)` but for this code it really isn't any different. – Dietmar Kühl Mar 18 '12 at 22:14
  • It's because `` isn't in the C++ standard. – ipc Mar 18 '12 at 22:24
  • @ipc: You must have a newer version of the C++ standard than I do. Mine clearly states in D.5 "C standard library headers" [depr.c.headers] "For compatibility with the C standard library ... the C++ standard library provides the 25 C headers, as shown in Table 154. ... ..." Using something from clause D may be frowned upon but it is still a mandatory part of a standard conforming C++ implementation. This particular section is **very** unlikely to go away! – Dietmar Kühl Mar 18 '12 at 22:31
1
int main()
{

    ofstream of("xyz.txt");

    clrscr();
    ifstream inf;
    char line;
    inf.open("abc.txt");
    int count=0;
    while(!inf.eof())
    {
        inf.get(line);
        if(line>=65 && line<=123)
        {
            cout<<line;
            line=line-32;
            of<<line;
            count++;
            cout<<line;
        }
    }
    cout<<count;
    getch();
    return 0;
}
Erik
  • 2,316
  • 9
  • 36
  • 58
1

I found the answer to my own question. I really didn't want to use transform, but that does work as well. If anyone else stumbles across this here is how I figured it out...

#include <iostream>
#include <string>
#include <fstream>

int main()
{
std::ifstream theFile;
theFile.open("test.txt");
std::string theLine;
while (!theFile.eof())
{
  theFile >> theLine;       
  for (size_t j=0; j< theLine.length(); ++j)
  {
    theLine[j] = tolower(theLine[j]);
  }
  std::cout<<theLine<<std::endl;
     } 

 return 0;
}
user1128637
  • 227
  • 3
  • 5
  • 13
0

First of all, unless this is something like a homework assignment, it's probably easier to process one character at a time rather than one word at a time.

Yes, you have pretty much the right idea for converting to lower case, with the minor detail that you normally want to cast the input to unsigned char before passing it to tolower.

Personally, I'd avoid doing explicit input and output, and instead do a std::transform with a pair of istream_iterators and an ostream_iterator for the result.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Note that you'd need to turn off skipping of whitespace (e.g. using `std::noskipws`) when using `std::istream_iterator` because otherwise all spaces will be eaten. Specifically for the character type this problem can be avoided and the code be made more efficient by using `std::istreambuf_iterator` (note the extra `buf`). – Dietmar Kühl Mar 18 '12 at 21:51