language: C (gcc-4.7.2)
date: 590 days 9 hours ago
link:
visibility: public
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#define n ((sizeof(char)) * 100 )
 
 
// saippuakalasalakauppias 
// saippuakuppinippukauppias 
int stringlength(char * str)
{
    int count=0;
    while(*str)
    {  
               if(*str=='\n')
               {
                     *str=0;
               }
               else
               count++,str++;
 
    }
    return count;
}
 
 
int palin1(char *str, int k)
{
    char * pend = str + k - 1;
    if(*pend!=*str)
    return 0;
    else  palin1(str+1,k-1);
    return 1;
}    
 
int palin(char *str)
{
    int length= stringlength(str),f=0;
    char *pend = str + length - 1;
    while(str<=pend)
    {
          if(*str==*pend) f=1;
          else
          return (f=0);
    str++,pend--;
    }
}
 
main()
{
 
      char * ps = (char *)malloc(n);
      int flag;
      if(ps==NULL) printf("Malloc Fail\n");
      else
      {
          printf("Malloc Succeeded, you have memory of %d bytes\n",n);
          printf("This program checks if String is Palindrome or not\n\
          \nEnter your String: ");
          fgets(ps,100,stdin);
          printf("You entered: %s of length %d",ps,stringlength(ps));
          int i=0;
          printf("\n\nEnter:\n1.Using iteration\n2.Using Recursion ");
          scanf("%d",&i);
          switch(i)
          {
                   case 1:
                          flag=palin(ps);
                          break;
                   case 2:
                          flag=palin1(ps,stringlength(ps));
                          break;
                   default:
                           printf("Invalid input");
          }
          
          if(flag) printf("\nYou entered a Palindrome");
          else  printf("\nNot a Palindrome");
      }
      free (ps);
    return 0;
}