palindrome using pointers;

Q) Write a program that prints whther a given string is a palindrome or not.
write one function void checkPalindrome(char *str)

Can somebody guide with the following code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
#include <iostream>
using namespace std;

void checkPalindrome(const char* p){
    
    const char* pnew = p;
    
    for(p;*p != '\0';p++);
  //  p--;
    while (*pnew++ == *--p);
    
    if(pnew>p) cout<<"is";

    else cout<<"isnot";
    
    
}
int main()
{
    int x;
    cout<<"enter the size of the string ";
    cin>>x;
    
    char* str = new char[x];
    
    for(int i = 0; i<x; i++){
        
        cin>>str[i] ;
    }
    
    const char* pointer;
    
    pointer = str;
    
    checkPalindrome(pointer);
    
    delete [] str;
   
    
    
}



The above code is only checking palindrome of 6 characters properly and
if i remove commenting on line 10 only 5 characters are checked .

closed account (48T7M4Gy)
1. Why not just write cin>> str; instead of lines 27-30 inc?

2. Try also checkPalindrome( str ); at line 36?

:-)
after doing that even the 5 character word is not working….. can tell what is wrong with my checkPalindrome code ?
closed account (48T7M4Gy)
Show us your code. I am runningyour code successfully on codeblocks for all sorts of lengths including 5.

PS with the couple of small changes I suggested. (Of course.)

PPS I didn't check your function logic because it worked. I'm lazy. But if it aint broken ...
Last edited on
Ohh yes its running fine ..!?!! thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

void checkPalindrome(const char* p){
    
   const char* pnew = p;
  
    while(*++p);
  //  *p--;
    while (*pnew++ == *--p);

    if(pnew>p){
        cout<<"is";
    }
    else{
        cout<<"isnot";
    }
    
}
int main()
{
    int x;
cout<<"enter the size of the string ";
    cin>>x;
    
    char* str = new char[x];

   // for(int i = 0; i<x; i++){
        
        cin>>str ;
    //}
    
  // const char* pointer;
    
    checkPalindrome(str);
    
    delete [] str;
    
    
    
}

is there some way so that i can prevent the users from writing more than they entered .That is

if i entered size 2,
I will still be able to write more than 2 characters and the function is checking all of them !

for eg:

size entered : 2;
str entered : abcddcba;
output : is

how can i prevent this …

closed account (48T7M4Gy)
One way is to accept a full line of text by getline() or whatever and then check that the length of that input complies before proceeding ...
Last edited on
1) I suggest to read string in std::string and then call checkPalindrome(str.c_str())

2) Use const char* pnew = p + std::strlen(p); to make pnew pont to the end of the line.

3) while (*pnew++ == *p--); Has a danger of going out of bounds. Add sanity check for pointer:
1
2
while (*pnew++ == *p-- && p <= pnew)
    ;
Topic archived. No new replies allowed.