| | |
Question about reversal and array storage for Palindrome
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 3
Reputation:
Solved Threads: 0
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:
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...
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
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.
now all you have to do is creae another std::string object and copy the caracters backwards from inbuf into it.
You do loop is all wrong. Use a while loop instead for better control.
C++ Syntax (Toggle Plain Text)
std::string inbuf; while( getline(infile,inbuf) ) { // blabla }
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.
•
•
Join Date: Nov 2006
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
std::string inbuf; while( getline(infile,inbuf) ) { // blabla }
now all you have to do is creae another std::string object and copy the caracters backwards from inbuf into it.
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:
"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:
C++ Syntax (Toggle Plain Text)
for(int i = inbuf.length()-1; i >= 0; --i) { }
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.
•
•
Join Date: Nov 2006
Posts: 3
Reputation:
Solved Threads: 0
the letters. The letters have to be reversed so that it can be compared to the normal line.
![]() |
Similar Threads
- Little Simple array question :) (C)
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- palindrome (Java)
- Array in talbe (problem) (C)
Other Threads in the C++ Forum
- Previous Thread: Function question
- Next Thread: quick question
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






