reverse words

hi guys please iv done my best involving few people already. program has to pick up words from 1file plikwe.txt, reverse them leaving a white spaces in a right places and then putting into plikwy.txt

it works a little bit different please guys help me adding a code dont tell what to do please and many thanks! :

include <cstdlib>
include <iostream>
include <fstream>

using namespace std;

int main (int argc,char *argv[])
{
const string delims("\t,.; ");
string line;
ifstream infile;
ofstream outfile;

plikwe.open("we.txt");
plikwy.open("wy.txt");

while(getline(plikwe,line)){
string::size_type begIdx,endIdx

begIdx=line.find_first_not_of(delims);

while(begIdx!=string::npos)
{
endIdx=line.find_first_of(delims,begIdx);

if(endIdx=string::npos){
endIdx=line.length();
}

for(int i=endIdx-1;i>=static_cast<int>(begIdx);--1) /*chce to zastapic czyms prostszym napisal mi to kto inny na moje mozliwosci ten static_cast jest za ciezki/

plikwy<<line[i];
}

plikwy<<'';

begIdx=line.find_first_not_of(delims,endIdx);
}
plikwy<<endl;
}

plikwe.close();
plikwy.close();

return();
}
plikwe.open("we.txt");
plikwy.open("wy.txt");

change to:

infile.open...

while(getline(plikwe,line))

change to :

while (getline(infIle, line))

the declaration of ifstream infile; creates a variable called "infile" (an object of input_file_stream class )...this is what you use to do file operations.
because you are trying to open 2 different files you may want to create 2 variables one for each file. unless you are going to process one, then close it, then process the other file.

EDIT: instead of changing the above you can just change:
ifstream infile;
ofstream outfile;

to

ifstream plikwe;
ofstream plikwy;

you should also test to make sure the file opened correctly. eg.

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
#include <cstdlib>
using std::exit;
// so we can exit program on conditions

int main ()
{
const string delims("\t,.; ");
string line;
ifstream plikwe;
ofstream plikwy;

plikwe.open("plikwe.txt");
plikwy.open("plikwy.txt");// you said they were called, plikwe and plikwy yet your 
// trying to open we.txt /wy.txt this will fail, unless you change the filenames.

if (! plikwe.is_open())
{
   cout << "ERROR: unable to open input file";
   exit (1);
}
else if (! plikwy.is_open())
{
   cout << "ERROR: unable to open output file";
   exit (1);
}


// rest of code goes here... 
Last edited on
This function here will return a reversed version of the string passed in as parameter:

1
2
3
4
5
6
7
8
9
10
std::string reverseWord(std::string word)
{
	char *wordArray = new char[word.length()];
	for (int i = 0; i < word.length(); ++i)
	{
		wordArray[i] = word.at(word.length() - (i + 1));
	}
	std::string returnString(wordArray);
	return returnString;
}


Now just read whatever words you want to reverse from the file, call this function on them and output the returned string to the other file.
I guess it should be asked. are you trying to reverse strings? eg:
hello world my name is bob -> bob is name my world hello

if not then the above beibin listed will work with some modification.
If you want to reverse the characters in a single word, there is an STL algorithm that can do it for you.

For example:
1
2
3
4
5
#include <algorithm>
// ...
string s = "asdf";
reverse( s.begin(), s.end() );
// s now contains "fdsa" 


I think the reverseWord function posted above has a few issues. For one, the memory allocated by new is not being deleted. Additionally, wordArray is not null terminated, so returnString may not be constructed properly unless the length is also passed in.
guys i really do appreciate ur effort..just came back from work and let me see what i can do with ur support...

btw expect me to come back ;)

and sorry some comments were in polish i havent noticed what a moron..
Topic archived. No new replies allowed.