Question about reversal and array storage for Palindrome

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 3
Reputation: xerA is an unknown quantity at this point 
Solved Threads: 0
xerA xerA is offline Offline
Newbie Poster

Question about reversal and array storage for Palindrome

 
0
  #1
Nov 3rd, 2006
Hi everyone.

I'm extremely new to this. It's my first programming course, period. I realize I probably needed a load of pre-reqs, but for some reason, I was advised to take Intro to C++ so...

So far, I've been doing well! However, this latest project has me stumped...

The jist of it:

Read lines from an input file, reverse, compare, find palindrome, and write it back out to the input file.

So far:

 
#include<fstream>
#include<iostream>
#include<string>
#include<cctype>
usingnamespace std;
#define SIZE 80
char palinCan[SIZE+1];
char reverse[SIZE+1];
int i = 1,a = 0, j ,q = 0,num = 0;
char reversal(char palinCan[SIZE]);
char b;
 
int main()
{
//int palindrome(char[]);
 
ifstream in;
ifstream infile;
infile.open("file.txt.txt", ios::in);
if (infile.fail())
{
cerr << "Couldn't open file..." << endl;
return 0;
}
do{
 
infile.getline(palinCan, SIZE+1);
strcpy_s(reverse, palinCan);
/* if(palindrome(reverse))
cout << storage << reverse << "It's a palindrome" << endl; 
else 
cout << "Nope" << endl; // I am getting "does not evaluate to function taking #of arguments here */
cout << palinCan << reverse << endl;
 
 
}while (!infile.eof());
}
char reversal()
{
for(j = strlen(palinCan); j >= 0 ; j--){
cin >> reverse[SIZE];}
return reverse[SIZE];
}

I don't know how to make a functioning... function to reverse the loop and return the result into the reversal array for comparison. I've tried a few variations, but none of them worked.

Once I have the reverse of the string stored in the reverse array, I should be able to make a comparison function with it, and have it return a simple true or false.

I appreciate any help...
Last edited by xerA; Nov 3rd, 2006 at 10:27 pm. Reason: spelling and code fix
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Question about reversal and array storage for Palindrome

 
0
  #2
Nov 3rd, 2006
This is a c++ program, why are you using c-style char arrays. Unless your instructore requires it you should probably be using std::string as input buffer.

You do loop is all wrong. Use a while loop instead for better control.

  1. std::string inbuf;
  2. while( getline(infile,inbuf) )
  3. {
  4. // blabla
  5. }

now all you have to do is creae another std::string object and copy the caracters backwards from inbuf into it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 3
Reputation: xerA is an unknown quantity at this point 
Solved Threads: 0
xerA xerA is offline Offline
Newbie Poster

Re: Question about reversal and array storage for Palindrome

 
0
  #3
Nov 3rd, 2006
Originally Posted by Ancient Dragon View Post
This is a c++ program, why are you using c-style char arrays. Unless your instructore requires it you should probably be using std::string as input buffer.

You do loop is all wrong. Use a while loop instead for better control.

  1. std::string inbuf;
  2. while( getline(infile,inbuf) )
  3. {
  4. // blabla
  5. }

now all you have to do is creae another std::string object and copy the caracters backwards from inbuf into it.
How do I copy the reverse/backwards into it though? That's the primary problem I'm having now... getting the reverse of a string and storing that for comparison.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Question about reversal and array storage for Palindrome

 
0
  #4
Nov 3rd, 2006
do you have to reverse just the words?
"Hello World" becomes "World Hello"

or reverse all the letters? Reversing the letters is easy -- reversing words becomes a little more complex. As I mentioned in my previous post, you need to create a second string and copy the characters to it in reverse order -- create a loop that starts at the end of the string and decrement instead of increment the loop counter. For example:
  1. for(int i = inbuf.length()-1; i >= 0; --i)
  2. {
  3.  
  4. }
Last edited by Ancient Dragon; Nov 4th, 2006 at 12:02 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 3
Reputation: xerA is an unknown quantity at this point 
Solved Threads: 0
xerA xerA is offline Offline
Newbie Poster

Re: Question about reversal and array storage for Palindrome

 
0
  #5
Nov 4th, 2006
Originally Posted by Ancient Dragon View Post
do you have to reverse just the words?
"Hello World" becomes "World Hello"

or reverse all the letters? Reversing the letters is easy -- reversing words becomes a little more complex.
the letters. The letters have to be reversed so that it can be compared to the normal line.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Question about reversal and array storage for Palindrome

 
0
  #6
Nov 4th, 2006
Originally Posted by xerA View Post
the letters. The letters have to be reversed so that it can be compared to the normal line.
Good -- that simplifies the program. See my previous post. The rest you will have to figure out because I don't want to write your program for you.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC