Command Line Program to Check if a String is Palindrome or Not

It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later.

#include<stdio.h>
#include<stdlib.h>
void isPalindrome(char str[])
{
    int l = 0;
    int h = strlen(str) - 1;
    while (h > l)
    {
        if (str[l++] != str[h--])
        {
            printf("%s is Not Palindromen", str);
            return;
        }
    }
    printf("%s is palindrome", str);
}

int main(int argc, char *argv[])
{
    int i,k;
    int strsize = 0;
    for (i=1; i i+1)
        strsize++;
    }
    char *cmdstring;
    cmdstring = malloc(strsize);
    cmdstring[0] = '\0';
    for (k=1; k k+1)
        strcat(cmdstring, " ");
    }
    isPalindrome(cmdstring);
}

 

One comment on “Command Line Program to Check if a String is Palindrome or Not”


  • Rishabh

    #include
    #include
    int main(int argc, char *argv[])
    {
    isPalindrome(argv[1]);
    return 0;
    }

    void isPalindrome(char str[])
    {
    int i,length;
    int flag=0;
    length=strlen(str)-1;
    for(i=0;i<length;i++)
    {
    if(str[i]!=str[length-i])
    {
    flag=1;
    break;
    }
    }
    if(flag==1)
    {
    printf("string is not palindrome");
    }
    else
    {
    printf("string is palindrome");
    }
    }

    simplest way