#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.

Recommended Answers

All 5 Replies

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

#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)

  1. Don't use gets(). It's dangerous. (Buffer overruns.)
  2. In C++, <stdio.h> becomes <cstdio>.
  3. <conio.h> is non-standard. You're not using it anywhere.
  4. <iostream.h> is also deprecated. Use
    #include <iostream>
    using namespace std;

    instead.

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).)

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().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.