forked from namita2310/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappends_needed.cpp
52 lines (42 loc) · 927 Bytes
/
appends_needed.cpp
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
// C program to find minimum number of appends
// needed to make a string Palindrome
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
// Checking if the string is palindrome or not
bool isPalindrome(char *str)
{
int len = strlen(str);
// single character is always palindrome
if (len == 1)
return true;
// pointing to first character
char *ptr1 = str;
// pointing to last character
char *ptr2 = str+len-1;
while (ptr2 > ptr1)
{
if (*ptr1 != *ptr2)
return false;
ptr1++;
ptr2--;
}
return true;
}
// Recursive function to count number of appends
int noOfAppends(char s[])
{
if (isPalindrome(s))
return 0;
// Removing first character of string by
// incrementing base address pointer.
s++;
return 1 + noOfAppends(s);
}
// Driver program to test above functions
int main()
{
char s[] = "abede";
printf("%d\n", noOfAppends(s));
return 0;
}