954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

98% done

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

int main()
{   void palcheck(char str[]);   
    char ans;
    char str[20];
    do
    {
     
     cout<<"Enter the string to check if it is a palindrome. ";
     gets(str);
     palcheck(str);
     cout<<"\nEnter Y to continue. ";
     cin>>ans;
    }while (ans=='y'||ans=='Y');  
     
    
}
void palcheck(char str[])
{    int l=0,k=0,flag;
     for(int i=0;str[i]!='\0';i++)
    l=l+1;
    k=l;
    for(int j=0;j<l/2;j++)
    {
            k--;
            if(str[j]==str[k])
            flag=1;
            else
            {flag=0;
            break;}
    }
    if(flag==0)
    cout<<"\nString is not a palindrome. ";
    else cout<<"String is a palindrome. ";
}

i am almost done . just a small error. it is not asking for new input which i want it to do . it is storing the previous value. some problem around the do loop but not able to figure out. i think it has to do something with declaration of variable.

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

void palcheck(char str[]);
needs to be written before main, not inside of it.

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int main()
{   void palcheck(char str[]);   
    char ans;
    char str[20];
    do
    {
     
     cout<<"Enter the string to check if it is a palindrome. ";
     gets(str);
     palcheck(str);
     cout<<"\nEnter Y to continue. ";
     cin>>ans;
    }while (ans=='y'||ans=='Y');  
     
    
}
void palcheck(char str[])
{    int l=0,k=0,flag;
     for(int i=0;str[i]!='\0';i++)
    l=l+1;
    k=l;
    for(int j=0;j<l/2;j++)
    {
            k--;
            if(str[j]==str[k])
            flag=1;
            else
            {flag=0;
            break;}
    }
    if(flag==0)
    cout<<"\nString is not a palindrome. ";
    else cout<<"String is a palindrome. ";
}

i am almost done . just a small error. it is not asking for new input which i want it to do . it is storing the previous value. some problem around the do loop but not able to figure out. i think it has to do something with declaration of variable.

Argh, stop with the school work already... And dont use gets, se man gets... Well the code looks not as borked as I first thought, but its pretty damn borked anyway I just dont have gdb installed so I'm not sure I can pinpoint the exact reason... However a educated quess is that the cin >> only eats the 'y' and not the following \n getting stuck in the gets, it does not explain why the same value should occur more than once though since that should output a zero length string, could be some odd thing happening with the buffering when both new and old io is used (not so on my compiler though)

perniciosus
Junior Poster in Training
78 posts since Nov 2005
Reputation Points: 29
Solved Threads: 4
 
  • Don't use gets(). It's dangerous. (Buffer overruns.)
  • In C++, becomes .
  • is non-standard. You're not using it anywhere.
  • is also deprecated. Use

    #include <iostream>
    using namespace std;

    instead.

dwks
Posting Whiz in Training
269 posts since Nov 2005
Reputation Points: 185
Solved Threads: 28
 

You need to clear the input buffer after the gets. (As I said, you shouldn't even use gets(). Use getline instead (in C++, fgets() in C).)

dwks
Posting Whiz in Training
269 posts since Nov 2005
Reputation Points: 185
Solved Threads: 28
 
void palcheck(char str[]); needs to be written before main, not inside of it.


Wrong. It can go inside main, but like a variable, it will only be visible in main().

dwks
Posting Whiz in Training
269 posts since Nov 2005
Reputation Points: 185
Solved Threads: 28
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You