Be a Supporter!

How to write this C++ program?

  • 1,396 Views
  • 12 Replies
New Topic Respond to this Topic
Magic8Ball
Magic8Ball
  • Member since: Feb. 25, 2004
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
How to write this C++ program? 2006-04-19 07:13:44 Reply

My assignment:
------------------------------------------
------------------------------------------
------------

Develop a C++ program to display a message indicating if each of the words, phrases, or
sentences in the file "pdrome.txt" is a palindrome or not. Each line in "pdrome.txt" contains a single word, phrase, or sentence. The output will be displayed on the monitor and saved in the file “output.txt”.
Note: Upper-case and lower-case letters will be treated the same.

"pdrome.txt" contains:
5 Level 5
Ma da m
ABBA
Able was I ere I saw ELBa
A MAN, A Plan, A Canal Panama
Palindrome
a&^% $#()+@ V$)(* A

------------------------------------------
------------------------------------------
------------

My head be spinning, yo. @_@ Just don't know where to begin..

CyberLemming
CyberLemming
  • Member since: Aug. 9, 2005
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to How to write this C++ program? 2006-04-19 07:37:33 Reply

You would need to open the file, then read each line at a time. check if the lowercased word is equal to the lowercased backwards word, and if so print that out. then move to the next line.

jarrydn
jarrydn
  • Member since: Jul. 15, 2002
  • Offline.
Forum Stats
Moderator
Level 10
Artist
Response to How to write this C++ program? 2006-04-19 08:31:10 Reply

I won't be able to help you too much because I'm much more familiar with Java than C, but what you'll want to do, is determine the length of the string, then write a for loop that takes the last letter from the string and puts it into a new string, then keeps working backwards. Once you done that, you write a for loop which compares the char at a particular place in both the strings to see if they are equal.

----------
for (i = 0, i <= stringlegnth, i ++)
tempchar1 = string1.charAt(i)
tempchar 2 = string2.charAt(i)

if tempchar1 != tempchar2
print "string not a palindrome"
break
else
if i == stringlength
print "string is a palindrome"
----------

or something like that. correct me if I'm wrong.


audio / bbs troubles? drop me a PM

BBS Signature
Magic8Ball
Magic8Ball
  • Member since: Feb. 25, 2004
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to How to write this C++ program? 2006-04-19 10:42:46 Reply

Yeah, both of those sound like the right way to go. I had psuedocoded it out so I could think about it:

1. Read lines from pdrome.txt
2. Remove all casing, spaces, and punctuation
3. Store a reverse of each line
4. Compare each line to it's reverse
5. Print results, indicating whether or not each line is a palindrome

I'm just too new to C++ programming. I don't know the specific commands and junk.. :(

Magic8Ball
Magic8Ball
  • Member since: Feb. 25, 2004
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to How to write this C++ program? 2006-04-19 12:04:58 Reply

It would be sweet if someone could code this psuedocode into C++, or just get me started. Cause I don't know how to code it. (with strings? arrays of strings maybe?)

Magic8Ball
Magic8Ball
  • Member since: Feb. 25, 2004
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to How to write this C++ program? 2006-04-19 15:13:24 Reply

Class in two hours, haha. I'm so screwed.

Jcrypt
Jcrypt
  • Member since: Aug. 23, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to How to write this C++ program? 2006-04-19 15:41:40 Reply

what about something like.......
// using <fstream> / <iostream> / <string>headers of course
ifstream inFile("paladin.txt");
ofstream outFile("output.txt", ios::ate);
string out;
if(inFile.is_open()){
for(int i = 0;i < inFile.eof(); i++){
out += inFile[i];
outFile << inFile[i];
// reverse string using revers() i believe
// break outta loop after end of file reached...

Sorry, it's not proper syntax etc just writing a psuedo as to which direction i'd take on it.... good luck hope you get it in time.

Ravens-Grin
Ravens-Grin
  • Member since: Jun. 3, 2003
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to How to write this C++ program? 2006-04-19 21:14:40 Reply

At 4/19/06 03:13 PM, Magic8Ball wrote: Class in two hours, haha. I'm so screwed.

Started too late, sorry man.

But I wrote a palindrome checking program in Java, and it used one stack and one array, and it just went through each one seeing if they were equal to each other. Oh, and for the queue, the way you load it is from the first letter you push that onto the stack, and so on and so forth.

0x41
0x41
  • Member since: Dec. 30, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to How to write this C++ program? 2006-04-19 21:41:20 Reply

At 4/19/06 03:41 PM, Jcrypt wrote:
// reverse string using revers() i believe

There is no reverse function in C++. Personally, I prefer the triple XOR method for reversing strings:

char Blah[11] = "Newgrounds";
int iLength = strlen(Blah) - 1;
for(int i = 0; i<iLength; i++, iLength--)
{
Blah[i]^=Blah[iLength];
Blah[iLength]^=Blah[i];
Blah[i]^=Blah[iLength];
}
printf("%s\n", Blah);

FPSinsanity
FPSinsanity
  • Member since: Jul. 11, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to How to write this C++ program? 2006-04-19 23:00:07 Reply

You wouldn't necessarily need to reverse the string. If 'i' were your incrementing variable and 'length' was the length of the string, this would do the trick:

for( i = 0; i < length/2; ++i )
{
if( string[i] != string[length-i] ) FAIL;
}
SUCCEED!

If the word makes it through the entire loop, it's a palendrome. No reversals necessary.

This extends from the idea that palendromes pivot around their center, with the letters the same distance from the center being the same.

perj
perj
  • Member since: Dec. 9, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to How to write this C++ program? 2006-04-19 23:28:42 Reply

At 4/19/06 09:41 PM, 0x41 wrote:
At 4/19/06 03:41 PM, Jcrypt wrote:
// reverse string using revers() i believe
There is no reverse function in C++. Personally, I prefer the triple XOR method for reversing strings:

char Blah[11] = "Newgrounds";
int iLength = strlen(Blah) - 1;
for(int i = 0; i<iLength; i++, iLength--)
{
Blah[i]^=Blah[iLength];
Blah[iLength]^=Blah[i];
Blah[i]^=Blah[iLength];
}
printf("%s\n", Blah);

can you just tell me what XOR does, why you couldn't just make another string and say new_blan[i] = blah[iLength], and what are the advantages of using printf("") instead of cout?
Thanks.

humbug88
humbug88
  • Member since: Mar. 29, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to How to write this C++ program? 2006-04-20 03:41:42 Reply

At 4/19/06 11:28 PM, perj wrote: can you just tell me what XOR does, why you couldn't just make another string and say new_blan[i] = blah[iLength], and what are the advantages of using printf("") instead of cout?
Thanks.

Theres no real big advantage of using printf() instead of cout but some peopel prefer it. It doesnt realy matter unless you know the exact diferences and advantages.

You could just make a new string and say new_blan[i] = blah[iLength], but that is not needed. The second technique mentioned is probably the best.

For the syntax stuff look at http://newgrounds.co../topic.php?id=434835
(C++: main) and look up the stuff that you need. It's very helpful.

humbug (88)


BBS Signature
FPSinsanity
FPSinsanity
  • Member since: Jul. 11, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to How to write this C++ program? 2006-04-20 10:41:28 Reply

what are the advantages of using printf("") instead of cout?

Printf is part of the C subset that has been extended into C++. Printf is purely function based, which means you exclusively use the printf() function to produce output. The printf() function is also special (and a product of C) because it is a function that takes a variable number of arguments, a trend which C++ has seperated itself from.

Cout is a class based output system. Along with functions, it uses operator overloading (like << ) to display output. It also holds behavior variables inside of itself to remember the specifics of formatting.

Now, for the advantages and disadvantages of both.
1) Prinf() (Disadvantage) - If you forget to put the variables you are writing (or reading with scanf), you program will print garbage or crash at runtime.

2) Cout (Advantage) - Cout and Cin are less typecast because of the way operator overloading works. This means you don't have to specify the type of variable you want inside of a control string, like with printf().

3) Printf() (Advantage) - Formatting is inline and easier to achieve if you know what you are doing. Since formatting behavior is not remembered, like with cout, it's easier to format the text exactly the way you want it.

4) Personal preference - Both forms of input can accomplish the same goals. However, some people prefer one over the other. Personally, I prefer to use printf(), scanf(), and getchar() to handle my input and output, but I also code in C most of the time.