Hello!I have to check one txt file for simetrical words(like 1221 or abccba) and put these simetrical words into other file.End of word is ().,!? or space

I have figured out how to find out if string is simetrical.

int main() {
char s[100];
gets(s);
int len=strlen(s);
bool b=true;
for (int i=0;i<len;i++)
if (s[i]!=s[len-i-1]) b=false;
if (b) cout << "is!\n"; else cout << "is not\n";
system("Pause");
return 0;

i know how to read symbols one by one,but i have no idea how to make it stop when !?.,() is the symbol.

Recommended Answers

All 15 Replies

All you need to do is use an if statement that compares to one of those chars. If it matches, end the read loop and begin your comparison loop.

All you need to do is use an if statement that compares to one of those chars. If it matches, end the read loop and begin your comparison loop.

Like this?

while(!fin.eof()){
fin.get(ch);
if (ch=='!'||'?'||','||'.'||'('||')') break;

Like this?

while(!fin.eof()){
fin.get(ch);
if (ch=='!'||'?'||','||'.'||'('||')') break;

I think you have the idea, but the comparisons in the conditional will not work as written...

It should be:

if ('!' == ch || '?' == ch || ',' == ch || //etc.....

You will also want to store the chars to a small array so that you have a full "word" to work with. As written, your loop overwrites the same single char each time by rather than reading in and storing the "word".

So i have to make for loop?

arr=ch
i++

Your while loop should work, you just need to expand it a little. If you add the 2 lines you just mentioned to it, that should get you close. You will need a couple other lines outside the loop for initialization, etc. but that'll get you on the right track.

i have done this.Full of mistakes without writing into second file,because just dont know how to do that..

#include <iostream>
#include <fstream>


using namespace std;
bool b=true;
int arr[i];
char c;
int main() {

ifstream inFile;
inFile.open("example.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1);
}

while (!fin.eof()) {
     get.fin(ch)
if ('!' == ch || '?' == ch || ',' == ch || ','==ch || '('=ch || ')'==ch)
break;
else
{ch=arr[i]
i++;}
}

int len=i;
bool b=true;
for (int i=0;i<len;i++)
if (s[i]!=s[len-i-1]) b=false;}
}

inFile.close();
system("pause");
return 0;
}

In line 20, for one of the conditions you have used the = operator instead of the == operator.
= means assignment, where as == means checking for equality. Make the change, compile and run the code. Post what error you are getting
Also you might want to google for the function strtok

made this far.How to know if array is simetrical?i can do it only with strings.

#include <iostream>
#include <fstream>


using namespace std;
bool b=true;
int arr[i];
int i;
char c;
int main() {

ifstream inFile;
inFile.open("example.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1);
}

while (!fin.eof() && ch != '‰') 
{
     get.fin(ch)
if ((ch != '!' && ch != '?' && ch != ',' && ch != '(' && ch != ')' )
     {ch=arr[i]
     i++;}
else
     ch = '‰';
}

int len=i;
bool b=true;
for (int i=0;i<len;i++)
if (s[i]!=s[len-i-1]) b=false;}
}

inFile.close();
system("pause");
return 0;
}

What purpose does ch= arr achieve ?
Also when you hit space that means the word has been completed.... So check if the word is symmetrical at that point
On line 32 you have an array s.... where did that come from ?

that's all.But i dont know why example2.txt stilll remains empty after this..

in file example.txt i have put random text like:

anna anasana gvbmmf mhvfhgcd hgvjygkhgk 1221

Words in bold have to be written in example2.txt

#include <iostream>
#include <fstream>


using namespace std;

int main() {
int gar=0;
bool sim=true;
char simb;
char arr[40];
ifstream f1;
ofstream f2;
f1.open("example.txt");
if (!f1) {
cout << "Unable to open file";
exit(1);
}
f2.open("example2.txt");
if (!f2) {
cout << "Unable to open file";
exit(1);
}

while (!f1.eof()) 
{
     f1.get(simb);
if (simb != '!' && simb != '?' && simb != ',' && simb != '(' && simb != ')' )
     
     {
     arr[gar]=simb;
     gar++;}
else
     simb = '‰';

for (int i=0;i<gar;i++)
if (arr[i]!=arr[gar-i-1]) sim=false;
else sim=true;
}
if (sim)
{
ofstream f2 ("example2.txt"); 
if (f2.is_open()) 
{for(int i=0;i<gar;i++)
f2 << arr[i]<<endl; 
f2.close();
 
}
}

f1.close();
system("pause");
return 0;
}

Print your array on line 36.... Just before you enter the for loop

Print your array on line 36.... Just before you enter the for loop

What for?Will it make some difference?

I'm guessing to make sure it's populated...

Your formatting is horrible, so I'm not sure, but I think you're trying to open the file twice. Once on line 19, and again on line 42.

My bet is that it's erasing the contents of the file when it re-opens it.

i give up..

In one of my earlier posts I told you to check what function strtok does. The solution to your problem has 2 steps.
1. Use strtok to break the sentence into individual words
2. Check if individual word is a palindrome or not

Checking for palindrome is very simple. The main part of this problem is isolating individual words which strtok will do for you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.