Thread: bool palindrome definition

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    Exclamation bool palindrome definition

    Here what my program looks like now. I am trying to finish the bool palindrome definition. in the definiton i have shown what i am suppose to do but don't know how to write it out. all instructions are at the very bottom of the screen

    // A Program that lets a user enter a line of text and states
    // weather or not the line is a palindrome

    #include <iostream> // provides cout, etc.
    #include <fstream> // provides file functions
    #include <cstdlib> // provides exit()
    #include <cstring> // provides str len()
    using namespace std;

    const int MAXSTRINGSIZE= 80; // no more than 79 characters used
    const int SENTINEL[]="stop"; // used to end data entry

    void explain_prog(ostream& os);
    // Explains what the program does

    void read_string(istream& is,char s[],int maxlength);
    // postcondition: a string s of at most maxlength-1 characters has
    // been read from input stream is

    void write_string(ostream& os,const char s[]);
    // precondition: string s has a value
    // postcondition: s is written to output stream os

    void blank_remover(const char s1[],char s2[]);
    // precondition: s1 has a value
    // postcondition: s2 is s1 with all the blanks removed

    bool palindrome(const char s[]);
    // precondition: s is a string with all blanks removed
    // postcondition: value true returned if s is palindrome
    // otherwise false is returned

    int main()
    {
    char s1[MAXSTRINGSIZE], // holds one line of text
    s2[MAXSTRINGSIZE]; // s1 with all blanks removed

    bool pal; //true if s1 is a palindrome, otherwise false
    ofstream fout; // internal name for output file

    explain_prog(cout); // explain program to User at terminal

    // open output file - check for errors opening
    fout.open("pal.out");
    if(fout.fail())
    {
    cout << "\nOutput file opening failed!\n";
    exit(1);
    }

    // document output file
    fout << "\nJustin Cochrane CMSV1180\nAssignment #9 Question #1\n\n";
    explain_prog(fout);

    // get first line or SENTINEL
    cout<< "Please enter line of text or" <<SENTINEL<<endl;

    read_string(cin,s1,MAXSTRINGSIZE);

    while(strcmp(s1,SENTINEL)!=0)
    {
    fout<< "\nLine entered.";
    write_string(fout, s1);

    // remove blanks from s1 and store in s2
    blank_remover(s1,s2);

    pal=palindrome(s2);

    if(pal)
    {
    cout<< "line of text is a palindrome";
    fout<< "line of text is a palindrome";
    }
    else
    {
    cout<< "line of text is not a palindrome";
    fout<< "line of text is not a palindrome";
    }

    //get next line of text or SENTINEL
    cout<< "Please enter a line of text or"<<SENTINEL<<endl;
    read_string(cin,s1,MAXSTRINGSIZE);
    }

    // close files
    fout.close();


    return 0;
    }

    void explain_prog(ostream& os)
    {
    os << "\nThis program lets a user enter a line of text "
    << "and states weather it is a palindrome or not";

    }

    void read_string(istream& is, char s[], int maxlength)
    {
    char ch; //used to read eoln character
    is.get(s,maxlength);
    is.get(ch); //read eoln character

    }

    void write_string(ostream& os, const char s[])
    {
    os <<s<<endl;
    }

    void blank_remover(const char s1[],char s2[])
    {
    int n=strlen(s1); // characters in s1
    int j=0; // index for s2
    for(int i=0; i<n; ++i)
    {
    if(s1[i]!=' ')
    {
    s2[j]=s1[i];
    ++j;
    }
    }
    s2[j]='\0'; // put null character in s2
    }

    bool palindrome(const char s[])
    {
    bool pal=true; // assume s is a palindrome until mismatch is found

    **This is what i was told to do now
    // get the length n of s using the strlen() function
    // use a loop to see if s is a palindrome
    // for example, if n is 10 you would compare
    //s[0] with s[9]
    //s[1] with s[8]
    //s[2] with s[7]
    .
    .
    //s[i] with s[ fill in]
    // if any of the above don't match pal is set to false
    //you should return pal at the end of the function
    }



    ps. thank you for your help[/CODE]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Edit your post for code tags please
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    declare two ints, start and stop, to act as indices (aka a single char each) for the char array you are checking. initialize start to zero and stop to the quantitiy strlen(whateverYouAreChecking) minus 1. Declare a bool value and initialize it to true. then, while start less than stop if char represented by start not equal to char represented by stop, then not a palindrome so assign false to bool variable, and stop looking at rest of char. Otherwise increase start by one and decrease stop by one and check next pair of char.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    Exclamation reply to reply on bool defintion

    bool palindrome(const char s[])
    {
    bool pal=true; // assume s is a palindrome until mismatch is found

    int start=0,
    stop=strlen(s2[j-1]);

    bool=true;
    while(start>stop)
    {
    if(char start!char stop)
    {
    bool=false;
    }
    else
    {
    start++;
    stop--;
    }
    }
    }[CODE]I'am not sure if this is what you meant but could you clarify or possibly fix my errors

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM