Solved

c++ code help

Posted on 1998-08-13
2
162 Views
Last Modified: 2010-04-10
how do I read in a string from a file as a charactor array and then reverse the letters of each word ? e.g.

if the file contained :

This is a test
This is a test2

you would read in "This is a test" as a charactor array then reverse the letters to say "sihT si a tset" then read in the second line and reverse those letters as well.
0
Comment
Question by:astroyboyy
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1170261
1. open the file (fopen in file.h)

2. while not at the end of the file
i) read a line in to a character array (fread in file.h)
ii) use _strrev (most compilers have this, possibly without the leading underscore)in string.h to reverse the string

3. close the file (fclose in file.h)

0
 
LVL 11

Expert Comment

by:alexo
ID: 1170262
A C answer to a C++ question?  Duh...

STL makes the solution trivial.  Create a vector<string>, fill it using istream_iterators, use the for_each() function to invoke the reverse() function on all the strings in the vector.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question