Friday, 27 October 2017

To Check Whether String is Palindrome or Not


#include<stdio.h>

int main(int argc,char *argv[])
{
   int i,j,l,flag=1;
    if(argc!=2)
     exit(1);
    for(l=0;argv[1][l]!='\0';l++);
    j=l-1;
    for(i=0;i<l/2;i++)
    {
        if(argv[1][i]!=argv[1][j])
          {
            flag=0;
          }
        j--;
    }
    if(flag==1)
        printf("Palindrome");
    else
        printf("Not Palindrome");
    return 0;


}

6 comments:

  1. sir in above program u did not use atoi ....why? plz explain

    ReplyDelete
    Replies
    1. atoi is used to convert input command line argument into integer format
      since we are manipulating on strings && command line arguments are stored as strings we need not use atoi() method here

      atoi()-- convert to int format
      atol()--convert to long format
      atod()--convert to double format
      These methods are declared in

      Delete
  2. sir in above program u did not use atoi ....why? plz explain

    ReplyDelete
    Replies
    1. Hey this is a program for String and here there is no requirement of using atoi because atoi is used for converting string value to integer
      Suppose you pass 20 in command line then it will be treated as string "20", so to overcome that atoi is used so that it becomes 20.
      If we start doing some operation on string "20" + 5 then instead of 25 it give garbage value. But after using atoi(argv[1]) then doing any operation will give same result.
      In the question given above we just have to check whether the given string is palindrome or not, so first of all I am counting no of words in string then comparing first character with the last one. If all are Same till middle position of string then its palindrome.

      Delete
    2. sir when you are going to explain all these questions///please provide video solution

      Delete
  3. why this program is giving error using codeblock

    ReplyDelete

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...