Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 28th, 2004, 03:11 PM
Authorized User
 
Join Date: Jan 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default i need explanation ...

i have c++ book...and there is a question about checking if the strng is palindrome or not.....before i look into the solution at the last page of the book...i have tried alot to do it myself..but unfrtunately i couldnt do it........so..i pened the solution page and here is the solution of the book :

# include <iostream.h>
# include <conio.h>

int main()

{
    char text[80];
    int beg;;
    int back;
    int flag;
    int l;

    cout<<"Enter the string:"<<endl;
    cin>>text;

    l=0;


    while(text[l] != '\0')
        l++;
        flag=1;
        beg=0;
        back=l-1;

    while( (beg<=back) && flag )
    {
        if(text[beg] == text[back])
            flag=1;
        else
            flag=0;
        beg++;
        back--;
    }

    if(flag)

    cout<<"Yes its a palindrome"<<endl;
    else
        cout<<"No its not a palindrome"<<endl;
}


what i dont understand is the logic of this program really..!

int beg ...> is the beginig of the text
int back...> the end of the text....

why they are declared as integers?

int flag....> what is flag?
i didnt understand this word


thank you very much

Reply With Quote
  #2 (permalink)  
Old October 28th, 2004, 04:44 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi ahmed,
I try to explain it for you with help of a simple example,
suppose you enter your name,"ahmed"
so it stores in an array of chars
text[0]=a,text[1]=h,text[2]=m,text[3]=e,text[4]=d,text[5]='\0'
as you see in C++ the end of a string is defined as a '\0' character.
integer variable beg shows the beginning of the text(0),
integer variable back shows the end of the text(4),
integer variable l shows the length of the text(5),
now the algorithm starts to traverse from the beginning and the end of the text equally
Code:
    while( (beg<=back) && flag )
    {
        if(text[beg] == text[back])
            flag=1;
        else
            flag=0;
        beg++;
        back--;
    }
beg++ back--
----> <----
    a h m e d
in every loop beg increases one unit and back decreases one unit for checking the letters have the same distances from beginning and the end of the text.if there are two different letters the flag becomes 0 and your program exits from the loop cycle and tells you the text wasnt palindrome otherwise the flags remains 1 and the Conditional statement tell you the text was palindrome.
HtH.

--------------------------------------------
Mehdi.:)
Reply With Quote
  #3 (permalink)  
Old October 28th, 2004, 05:01 PM
Authorized User
 
Join Date: Jan 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

than you very much mehdi.....you explained it nicely.:)

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
code explanation Amitava Deewan Access VBA 2 August 8th, 2005 03:26 PM
explanation crmpicco Classic ASP Components 3 February 2nd, 2005 12:14 AM
Need Explanation Plz ! augustine Biztalk 0 July 11th, 2004 07:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.