Click to See Complete Forum and Search --> : Ugh...... problems with strings...


MEe
02-24-2001, 08:04 PM
I have this big homework problem, (NOT GRADED) so dont worry. But im completely
clueless on how to do it! I cant figure the for loops in the program to get
it to work. Heres the problem...

{
Write a program which would ask the user whether he wants to enter the string
or whether your program should read it from a file. If the case is the former,
ask the user to enter the string, if the case is the latter, ask the user
to enter the name of the file, open the file and read the string from the
file (you can assume the string can be up to 80 characters long).

Write a boolean function called palindrome which, when given a string, would
return true if the string is a palindrome and false if it is not. Print
out the result on the screen.

Your program should test only one such string: you do not need to have a
loop asking the user whether he wants to continue. Do NOT use global variables.

To check whther a string is a palindrome you need to check character for
character (we are only concerned with character palindromes) ignoring any
non-alphanumeric data. Thus the following are valid palindromes:

Madam, I'm Adam.
Eve
A man, a plan, a canal; Panama?
Naomi, did I moan?
Anna: "Did Otto peep?" Otto: "Did Anna?"
We repaid a no name Pacific ape man on a diaper, ew!
Doc, note: I dissent. A fast never prevents a fatness. I diet on cod.
}

Help or if its easy enough do a sample problem... Im NOT good at this C++
programming. But im trying :) I'll get there eventually. Computers dont like
like me hehe. Thnx for the help

_Kinnkade

Iniana Jonethon
02-24-2001, 09:58 PM
I had the exact same problem in my first sememster of C++. Don't worry kid,
c++ takes repetitions to get it down. The more coding you do of a certain
task, the more you understand it. It's hard to explain it to you so I'm
gonna type up a quick example. (Keep in mind, you can't just copy this and
expect it to be 100% perfect...this is used so that you can get an idea of
where to go.)
Sample:
#include<iostream>
#include<cctype> //to manipulate characters
#include<cstring> //to manipulate strings
#include<fstream> //to manipulate files

//don't declare anything here...If you do, then you'd have global variables

int main()
{
int option;
char myString[20]; //used to store the string
int strlngth; //used to store length of string
int rearpos; //used to store rear position being evaluated(you'll see)
int frontpos; //used to store font position being evaluated
string infile; //used to store name of file being inputted
ifstream filein; //used to read file (ifstream is "in file stream")

cout << "type 1 if you're typing string, or 2 if getting string by file"
<< endl;
cin >> option;

if (option == 1)
{
cout << "Type string." << endl;
cin.getline(myString, 20, '\n')
//'\n' is a newline character, cin will store the values typed
//until it gets to length of 20 or '\n', which ever comes first.
//Also, keep in mind that myString is an array of characters.
}
else if (option == 2)
{
cout << "type filename" << endl;
cin.getline >> infile; //read in file name
filein.open (infile.c_str); //open file with filename
filein >> myString; //read from file and store in myString
}

strlngth = strlen(myString); //Stores length of string in 'strlngth'

lastpos = strlngth -1; //strlngth-1 is the last position of string.

for (frontpos = 0; frontpos < (strlngth/2); frontpos = frontpos +1)
//Here's a for loop. Listen up cuz you'll be using for loops alot!
//The first argument (frontpos;) is just a statement, it can be
//anything. The second argument( i < (strlngth/2); ) is
//a "termination statement". Meaning when this statement becomes

//false, the loop is terminated. The last argument (frontpos++) is
//a statement that executes at end of loop, before it starts over

//again.
{
if(frontpos == lastpos)
{
cout << "This string is a palindrome." << endl;
frontpos = strlngth; //to terminate the loop
{
else if(myString[frontpos] == myString[lastpos])
{
rearpos = rearpos -1; //also can be coded as lastpos--;
//we're coding this to move rearpos down one
//don't have to mess with frontpos because

//it's already been done in the argument
//list above.
}
else if (myString[frontpos] != myString[lastpos])
{
cout << "This is not a palindrome." << endl;
frontpos = strlnght; //to terminate the loop
}
}
return 0;
}

hope this gives you a good sense of direction. If you have questions or
problems(there will be bugs in this program...always is) just email me at
JKrueck@hotmail.com I did get an A in that class. Just keep up with the
class and homeworks. Always, and I mean ALWAYS ask questions if you don't
understand something. I didn't understand half the stuff either.
And shame on you thinking the computer don't like you. Just treat it with
respect!!! (Meaning don't give too many things to do at once)

"MEe" <kinnkade@yahoo.com> wrote:
>
>I have this big homework problem, (NOT GRADED) so dont worry. But im completely
>clueless on how to do it! I cant figure the for loops in the program to
get
>it to work. Heres the problem...
>
>{
>Write a program which would ask the user whether he wants to enter the string
>or whether your program should read it from a file. If the case is the
former,
>ask the user to enter the string, if the case is the latter, ask the user
>to enter the name of the file, open the file and read the string from the
>file (you can assume the string can be up to 80 characters long).
>
>Write a boolean function called palindrome which, when given a string, would
>return true if the string is a palindrome and false if it is not. Print
>out the result on the screen.
>
>Your program should test only one such string: you do not need to have a
>loop asking the user whether he wants to continue. Do NOT use global variables.
>
>To check whther a string is a palindrome you need to check character for
>character (we are only concerned with character palindromes) ignoring any
>non-alphanumeric data. Thus the following are valid palindromes:
>
>Madam, I'm Adam.
>Eve
>A man, a plan, a canal; Panama?
>Naomi, did I moan?
>Anna: "Did Otto peep?" Otto: "Did Anna?"
>We repaid a no name Pacific ape man on a diaper, ew!
>Doc, note: I dissent. A fast never prevents a fatness. I diet on cod.
>}
>
>Help or if its easy enough do a sample problem... Im NOT good at this C++
>programming. But im trying :) I'll get there eventually. Computers dont
like
>like me hehe. Thnx for the help
>
>_Kinnkade
>
>

IndianaJonethon
02-24-2001, 10:18 PM
"Iniana Jonethon" <Jkrueck@hotmail.com> wrote:
>
>I had the exact same problem in my first sememster of C++. Don't worry
kid,
>c++ takes repetitions to get it down. The more coding you do of a certain
>task, the more you understand it. It's hard to explain it to you so I'm
>gonna type up a quick example. (Keep in mind, you can't just copy this and
>expect it to be 100% perfect...this is used so that you can get an idea
of
>where to go.)
>Sample:
>#include<iostream>
>#include<cctype> //to manipulate characters
>#include<cstring> //to manipulate strings
>#include<fstream> //to manipulate files
>
>//don't declare anything here...If you do, then you'd have global variables
>
>int main()
>{
> int option;
> char myString[20]; //used to store the string
> int strlngth; //used to store length of string
> int rearpos; //used to store rear position being evaluated(you'll see)
> int frontpos; //used to store font position being evaluated
> string infile; //used to store name of file being inputted
> ifstream filein; //used to read file (ifstream is "in file stream")
>
> cout << "type 1 if you're typing string, or 2 if getting string by file"
> << endl;
> cin >> option;
>
> if (option == 1)
> {
> cout << "Type string." << endl;
> cin.getline(myString, 20, '\n')
> //'\n' is a newline character, cin will store the values typed
> //until it gets to length of 20 or '\n', which ever comes first.
> //Also, keep in mind that myString is an array of characters.
> }
> else if (option == 2)
> {
> cout << "type filename" << endl;
> cin.getline >> infile; //read in file name
> filein.open (infile.c_str); //open file with filename
> filein >> myString; //read from file and store in myString
> }
>
> strlngth = strlen(myString); //Stores length of string in 'strlngth'
>
> lastpos = strlngth -1; //strlngth-1 is the last position of string.
>
> for (frontpos = 0; frontpos < (strlngth/2); frontpos = frontpos +1)
> //Here's a for loop. Listen up cuz you'll be using for loops alot!
> //The first argument (frontpos;) is just a statement, it can be
> //anything. The second argument( i < (strlngth/2); ) is
> //a "termination statement". Meaning when this statement becomes
>
> //false, the loop is terminated. The last argument (frontpos++)
is
> //a statement that executes at end of loop, before it starts over
>
> //again.
> {
> if(frontpos == lastpos)
> {
> cout << "This string is a palindrome." << endl;
> frontpos = strlngth; //to terminate the loop
> {
> else if(myString[frontpos] == myString[lastpos])
> {
> rearpos = rearpos -1; //also can be coded as lastpos--;
> //we're coding this to move rearpos down one
> //don't have to mess with frontpos because
>
> //it's already been done in the argument
> //list above.
> }
> else if (myString[frontpos] != myString[lastpos])
> {
> cout << "This is not a palindrome." << endl;
> frontpos = strlnght; //to terminate the loop
> }
> }
> return 0;
>}
>
>hope this gives you a good sense of direction. If you have questions or
>problems(there will be bugs in this program...always is) just email me at
>JKrueck@hotmail.com I did get an A in that class. Just keep up with the
>class and homeworks. Always, and I mean ALWAYS ask questions if you don't
>understand something. I didn't understand half the stuff either.
>And shame on you thinking the computer don't like you. Just treat it with
>respect!!! (Meaning don't give too many things to do at once)
You can also message me on AIM at IndianaJonethon

>
>"MEe" <kinnkade@yahoo.com> wrote:
>>
>>I have this big homework problem, (NOT GRADED) so dont worry. But im completely
>>clueless on how to do it! I cant figure the for loops in the program to
>get
>>it to work. Heres the problem...
>>
>>{
>>Write a program which would ask the user whether he wants to enter the
string
>>or whether your program should read it from a file. If the case is the
>former,
>>ask the user to enter the string, if the case is the latter, ask the user
>>to enter the name of the file, open the file and read the string from the
>>file (you can assume the string can be up to 80 characters long).
>>
>>Write a boolean function called palindrome which, when given a string,
would
>>return true if the string is a palindrome and false if it is not. Print
>>out the result on the screen.
>>
>>Your program should test only one such string: you do not need to have
a
>>loop asking the user whether he wants to continue. Do NOT use global variables.
>>
>>To check whther a string is a palindrome you need to check character for
>>character (we are only concerned with character palindromes) ignoring any
>>non-alphanumeric data. Thus the following are valid palindromes:
>>
>>Madam, I'm Adam.
>>Eve
>>A man, a plan, a canal; Panama?
>>Naomi, did I moan?
>>Anna: "Did Otto peep?" Otto: "Did Anna?"
>>We repaid a no name Pacific ape man on a diaper, ew!
>>Doc, note: I dissent. A fast never prevents a fatness. I diet on cod.
>>}
>>
>>Help or if its easy enough do a sample problem... Im NOT good at this C++
>>programming. But im trying :) I'll get there eventually. Computers dont
>like
>>like me hehe. Thnx for the help
>>
>>_Kinnkade
>>
>>
>