#include <iostream>
#include <fstream>
#include <cstring>
usingnamespace std;
bool symetry(char * x);
int main()
{
fstream fout("f1.txt", ios::out);
fstream fin("f.txt", ios::in);
int i=0;
char * arr;
arr=newchar[40];
char symbol;
if(!fin)cout<<"Unable to open fin"<<endl;
if(!fout)cout<<"Unable to open fout file"<<endl;
while(fin)
{ fin.get(symbol);
if ( symbol == ' ' || symbol == '!' || symbol == '?' || symbol == '.' || symbol == ',' || symbol == '(' ||symbol == ')' )
{
cout<<"Stoping at "<<arr<<endl;
if(symetry(arr))cout<<"Symetrical word has been found: "<<arr<<endl;
fout.write(arr,strlen(arr)-1);
fout<<flush;
arr[0] = '\0';
i=0;}
else
{
arr[i]=symbol;
i++;}
}
delete[]arr;
fin.close();
fout.close();
return 0;
}
bool symetry(char * x)
{
for(int y=0,z=strlen(x)-1;z>y;y++,z--)
{ cout<<x[y]<<" is being compared to "<<x[z]<<endl;
if (x[y]!=x[z]) returnfalse;
}
returntrue;
};
Output when input file contains "akka alfa beta ala". The output symbols after the real words change randomly when executing.
Stoping at akkał)y
a is being compared to y
Stoping at alfał)y
a is being compared to y
Stoping at betał)y
b is being compared to y
Process returned 0 (0x0) execution time : 0.044 s
Press any key to continue.
How to get rid of those symbols so I can actually find the symetry?
Or perhaps I should go some different route to achieve the task stated in the beginning?
Just add null terminator. This way the string is terminated and strlen works. But if you don't have a space at the end of the line you won't process the last word. Do you need to process the data one character at a time?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while(fin)
{ fin.get(symbol);
if ( symbol == ' ' || symbol == '!' || symbol == '?' ||
symbol == '.' || symbol == ',' || symbol == '(' ||symbol == ')' )
{
arr[i] = '\0'; // <----------------------- Added null terminator
cout<<"Stoping at "<<arr<<endl;
$ ./a.out
Stoping at akka
a is being compared to a
k is being compared to k
Symetrical word has been found: akka
Stoping at alfa
a is being compared to a
l is being compared to f
Stoping at beta
b is being compared to a
Stoping at ala
a is being compared to a
Symetrical word has been found: ala
$
Figured that out aswell while waiting for a response :> Also - yes it is a requirment to process the data one character at a time, even the main goal of the task I guess.
Current code (also ends the array when eof is reached now):
#include <iostream>
#include <fstream>
#include <cstring>
usingnamespace std;
bool symetry(char * x);
int main()
{
fstream fin("f.txt", ios::in);
fstream fout("f1.txt", ios::out);
int i=0;
char * arr;
arr= newchar [40];
char symbol;
if(!fin)cout<<"Unable to open input file"<<endl;
if(!fout)cout<<"Unable to open output file"<<endl;
while(fin)
{
fin.get(symbol);
if ( fin.eof() || symbol == ' ' || symbol == '!' || symbol == '?'
|| symbol == '.' || symbol == ',' || symbol == '(' ||symbol == ')' )
{
arr[i]='\0';
cout<<endl;
cout<<"Stoping at '"<<arr<<"' "<<endl;
if(symetry(arr)&&i>0)
{cout<<"Symetrical word has been found: "<<arr<<endl;
fout<<arr<<'\n';
fout<<flush;
}
arr[0] = '\0';
i=0;
}
else
{
arr[i]=symbol;
i++;
}
}
fin.close();
fout.close();
return 0;
}
bool symetry(char * x)
{
for(int y=0,z=strlen(x)-1;z>y;y++,z--)
{ cout<<x[y]<<" is being compared to "<<x[z]<<endl;
if (x[y]!=x[z]) returnfalse;
}
returntrue;
};
It works quite well but yet another requirment of the task is that the file should be read sequentialy and in my textbook it is defined as
1 2 3 4 5 6
Read the first value from file
WHILE (file hasn't ended)
{
Process the value read
Read the next value from file
}
Yet if I try to read the value first, then process it inside the while cycle and then read the next value inside it - the program goes into a loop and just keeps on returning
Adding " if (symbol == '\n ' || ...........)" at the part where it checks for the end of the "word" solves the crashing and errors from new lines but still the sequential reading part mentioned before puzzles me. Not sure of what causes that loop if I modify it as in the textbook example
fin.get(symbol);
while(fin) {
if ( fin.eof() || symbol == ' ' || symbol == '!' ||
symbol == '?' || symbol == '\n' || symbol == '.' ||
symbol == ',' || symbol == '(' ||symbol == ')' ) {
arr[i]='\0';
cout<<endl;
cout<<"Stoping at '"<<arr<<"' "<<endl;
// I put the i check first because if it fails there
// is no need to call symetry
if(i>0 && symetry(arr)) {
cout<<"Symetrical word has been found: "<<arr<<endl;
fout<<arr<<'\n';
fout<<flush;
}
arr[0] = '\0';
i=0;
}
else {
arr[i]=symbol;
i++;
}
fin.get(symbol);
}
Stoping at 'ala'
a is being compared to a
Symetrical word has been found: ala
Stoping at 'ddd'
d is being compared to d
Symetrical word has been found: ddd
Stoping at 'iffi'
i is being compared to i
f is being compared to f
Symetrical word has been found: iffi
Stoping at 'abbba'
a is being compared to a
b is being compared to b
Symetrical word has been found: abbba
Stoping at 'abbcbba'
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbcbba
Stoping at 'this'
t is being compared to s
Stoping at 'file'
f is being compared to e
Stoping at 'is'
i is being compared to s
Stoping at 'used'
u is being compared to d
Stoping at 'with'
w is being compared to h
Stoping at 'Palindromes'
P is being compared to s
Stoping at 'akka'
a is being compared to a
k is being compared to k
Symetrical word has been found: akka
Stoping at 'alfa'
a is being compared to a
l is being compared to f
Stoping at 'abbxbba'
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbxbba
Stoping at 'abbybba'
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbybba
Stoping at 'abbzbba'
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbzbba
Stoping at 'abbabba'
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbabba
Stoping at 'abbbbba'
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbbbba
Stoping at 'abbcbba'
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbcbba
Stoping at 'aaa'
a is being compared to a
Symetrical word has been found: aaa
Stoping at 'wwwww'
w is being compared to w
w is being compared to w
Symetrical word has been found: wwwww
Stoping at 'wwwaww'
w is being compared to w
w is being compared to w
w is being compared to a
Stoping at 'wwwbwww'
w is being compared to w
w is being compared to w
w is being compared to w
Symetrical word has been found: wwwbwww
Stoping at 'wwwcwww'
w is being compared to w
w is being compared to w
w is being compared to w
Symetrical word has been found: wwwcwww
Stoping at 'wwwdwww'
w is being compared to w
w is being compared to w
w is being compared to w
Symetrical word has been found: wwwdwww
Stoping at 'wwwewww'
w is being compared to w
w is being compared to w
w is being compared to w
Symetrical word has been found: wwwewww
Stoping at 'xxxxx'
x is being compared to x
x is being compared to x
Symetrical word has been found: xxxxx
Stoping at 'nnnnn'
n is being compared to n
n is being compared to n
Symetrical word has been found: nnnnn
Stoping at 'xxxxxx'
x is being compared to x
x is being compared to x
x is being compared to x
Symetrical word has been found: xxxxxx
Stoping at ''
Stoping at ';xxxxxxx'
; is being compared to x
Stoping at 'xxxxxx'
x is being compared to x
x is being compared to x
x is being compared to x
Symetrical word has been found: xxxxxx
Stoping at ''
Stoping at ';xxqqjqqqxxxxpx'
; is being compared to x
Stoping at 'mmmmm'
m is being compared to m
m is being compared to m
Symetrical word has been found: mmmmm
Stoping at ''
Stoping at ''
Stoping at ''
Stoping at ''
Stoping at ''
Stoping at ''
Stoping at ''
Stoping at 'yyyyy'
y is being compared to y
y is being compared to y
Symetrical word has been found: yyyyy
Stoping at 'zzzzz'
z is being compared to z
z is being compared to z
Symetrical word has been found: zzzzz
Stoping at ''
Stoping at 'beta'
b is being compared to a
Stoping at ''
Stoping at 'ala'
a is being compared to a
Symetrical word has been found: ala
Stoping at 'ala'
a is being compared to a
Symetrical word has been found: ala
Stoping at 'ddd'
d is being compared to d
Symetrical word has been found: ddd
Stoping at 'iffi'
i is being compared to i
f is being compared to f
Symetrical word has been found: iffi
Stoping at 'abbba'
a is being compared to a
b is being compared to b
Symetrical word has been found: abbba
Stoping at 'abbcbba'
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbcbba
Stoping at ''
Stoping at 'yyyyyyyyyyyyy'
y is being compared to y
y is being compared to y
y is being compared to y
y is being compared to y
y is being compared to y
y is being compared to y
Symetrical word has been found: yyyyyyyyyyyyy
Stoping at 'zzzzzzzzzzzzz'
z is being compared to z
z is being compared to z
z is being compared to z
z is being compared to z
z is being compared to z
z is being compared to z
Symetrical word has been found: zzzzzzzzzzzzz
Stoping at '0000000000000'
0 is being compared to 0
0 is being compared to 0
0 is being compared to 0
0 is being compared to 0
0 is being compared to 0
0 is being compared to 0
Symetrical word has been found: 0000000000000
The problem was me taking the next symbol in the else part rather than in the main part of the while cycle. Silly me. Everything works as it should now, thank you all for the repsonses :)