C++ Palindrome Bool Function (Using Pointer)

ptaal

How to create a Palindrome function in C++? I'm using 2 functions types(bool and void). Here is my code so far (I would really appreciate any help with this, and why my code is not working?) Thank you!

#include <iostream>
#include <cctype>
#include <cstdlib>
#include <string>
using namespace std;


void reverse(char *);  
bool isPalindrome(char *);  

int main()  
{  
    char a[10];  
    cout << "string ";  
    cin.getline(a, 10);  
    if (palindrome(a))  
        cout << "true";   
    else  
        cout << "false";  
    return 0;  
}  
void reverse(char *s)  
{  
    char *point = s;  
    int i, min;  
    for (i = 0; *point != '\0'; i++)  
        point++;  
        min = i;  
        point--;  
    for (i = min; i > 0; i--)  
   {  
        cout << *point--;  
   }  
}  
bool ispalindrome(char *s)  
{  
    bool status;  
    char *original = s;  
    char *point = s;  
    reverse(point);        
    for (int i = 0; point != '\0'; i++)  
    {  
        if (point[i] == original[i])  
        status = true;  
        else  
        status = false;  
    }  
    return status;  
}
linluk

you dont need to reverse the string to check if its palindrome.

the algorithm works like:

get the length of the string;
loop from zero to length of string by 2;
compare the characters on position loop count with lenght minus loop count minus 1;
if the are not equal its not a palindrom;
its a palindrome if the loop finished;

for example: "test":
first step: compare 't' with 't'
second step: compare 'e' with 's' --> not a palindrom

for example "palap":
first step: compare 'p' with 'p'
second step: compare 'a' with 'a'
third step: compare 'l' with 'l'
now we know that it is a palindrom.

try thisone:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int palindrom(char * s){
  int i;
  int l = strlen(s);
  for(i=0;i<=l/2;i++)
    if(s[i]!=s[l-i-1]) return 0;
  return 1;
}

int main(void) {
  char * test = "test";
  char * pal = "palap";
  printf("%s %d", test, palindrom(test));
  printf("%s %d", pal, palindrom(pal));
  return 0; 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Palindrome function using recursion

From Dev

Using typedef function pointer in C

From Dev

Calling unmanaged C DLL function from C# (with pointer to struct and pointer to bool array)

From Dev

Using pointer as return value in function in c

From Dev

Using PInvoke in C# with function pointer and delegate

From Dev

C function change string using pointer

From Dev

passing function pointer to the C code using cgo

From Dev

C - using to pointer to function yield no result

From Dev

palindrome just using loops in C

From Dev

C/C++: using a typedef'd function pointer to *declare* a function

From Dev

How to run a function using a function pointer that located in a struct? (C)

From Dev

c - using a pointer returned from function in a function call

From Dev

How to run a function using a function pointer that located in a struct? (C)

From Dev

c - using a pointer returned from function in a function call

From Dev

c how to evaluate function pointer using function name

From Dev

c++ calling function when using map of function pointer

From Dev

Why does pointer to int convert to void* but pointer to function convert to bool?

From Dev

How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

From Dev

Recursion of Palindrome without using auxiliary function?

From Dev

Recursion of Palindrome without using auxiliary function?

From Dev

Pointer to function pointer in C++

From Dev

Unexpected output from palindrome function in C

From Dev

Is converting a bool (false) to a pointer legal in C++?

From Dev

Using function pointer as callback

From Dev

Using function pointer as callback

From Dev

c: when using a pointer as input in a function incrementing the pointers value by using *pointer++ doesn't work

From Dev

C++ pointer to member function not a function pointer

From Dev

Checking whether string is palindrome or not using Functions in C

From Dev

Python -passing imgdata pointer to a function in C using ctypes

Related Related

  1. 1

    Palindrome function using recursion

  2. 2

    Using typedef function pointer in C

  3. 3

    Calling unmanaged C DLL function from C# (with pointer to struct and pointer to bool array)

  4. 4

    Using pointer as return value in function in c

  5. 5

    Using PInvoke in C# with function pointer and delegate

  6. 6

    C function change string using pointer

  7. 7

    passing function pointer to the C code using cgo

  8. 8

    C - using to pointer to function yield no result

  9. 9

    palindrome just using loops in C

  10. 10

    C/C++: using a typedef'd function pointer to *declare* a function

  11. 11

    How to run a function using a function pointer that located in a struct? (C)

  12. 12

    c - using a pointer returned from function in a function call

  13. 13

    How to run a function using a function pointer that located in a struct? (C)

  14. 14

    c - using a pointer returned from function in a function call

  15. 15

    c how to evaluate function pointer using function name

  16. 16

    c++ calling function when using map of function pointer

  17. 17

    Why does pointer to int convert to void* but pointer to function convert to bool?

  18. 18

    How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

  19. 19

    Recursion of Palindrome without using auxiliary function?

  20. 20

    Recursion of Palindrome without using auxiliary function?

  21. 21

    Pointer to function pointer in C++

  22. 22

    Unexpected output from palindrome function in C

  23. 23

    Is converting a bool (false) to a pointer legal in C++?

  24. 24

    Using function pointer as callback

  25. 25

    Using function pointer as callback

  26. 26

    c: when using a pointer as input in a function incrementing the pointers value by using *pointer++ doesn't work

  27. 27

    C++ pointer to member function not a function pointer

  28. 28

    Checking whether string is palindrome or not using Functions in C

  29. 29

    Python -passing imgdata pointer to a function in C using ctypes

HotTag

Archive