Q:

C Program to check string is palindrome or not using pointers

0

Write a C Program to check string is palindrome or not using pointers. Here’s simple program to check whether a string is palindrome or not using pointers in C Programming language. C program to check if a string is palindrome or not using pointers.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What are Pointers?


A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.

 
 

The general form of a pointer variable declaration is −

  • type *var-name;

Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.

The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.

 

The unary or monadic operator & gives the “address of a variable’”.

The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.


Below is the source code for C Program to check string is palindrome or not using pointers which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C Program to check string is palindrome or not using pointers  */

#include <stdio.h>

int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);

int main()
{
        char string[100];
        int result;

        printf("Enter any string :: ");
        scanf("%s",string);

        result = is_palindrome(string);

        if ( result == 1 )
              printf("\n[ %s ] is a palindrome string.\n", string);
    else
              printf("\n[ %s ] is not a palindrome string.\n", string);

        return 0;
}

int is_palindrome(char *string) {
        int check, length;
        char *reverse;
        length = string_length(string);
        reverse = (char*)malloc(length+1);
        copy_string(reverse, string);
        reverse_string(reverse);
        check = compare_string(string, reverse);
        free(reverse);
        if ( check == 0 )
              return 1; else
              return 0;
}


int string_length(char *string) {
        int length = 0;
        while(*string) {
                length++;
                string++;
        }
        return length;
}


void copy_string(char *target, char *source) {
        while(*source) {
                *target = *source;
                source++;
                target++;
        }
        *target = '\0';
}


void reverse_string(char *string) {
        int length, c;
        char *begin, *end, temp;
        length = string_length(string);
        begin = string;
        end = string;
        for ( c = 0 ; c < ( length - 1 ) ; c++ )
               end++;
        for ( c = 0 ; c < length/2 ; c++ ) {
                temp = *end;
                *end = *begin;
                *begin = temp;
                begin++;
                end--;
        }
}


int compare_string(char *first, char *second) {
        while(*first==*second) {
                if ( *first == '\0' || *second == '\0' )
                         break;
                first++;
                second++;
        }
        if( *first == '\0' && *second == '\0' )
              return 0; else
              return -1;
}

Output  : :


/*  C Program to check string is palindrome or not using pointers  */

Enter any string :: RADAR

[ RADAR ] is a palindrome string.

Process returned 0

Above is the source code for C Program for Addition of Two Numbers Using Pointers which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Pointer Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to Get Address of array using Po... >>
<< C Program to Sort n numbers in ascending order usi...