Write a program which takes a string as input and prints the longest prefix of the string whose reverse is a valid suffix of the string. For example, if "notation" is given as input, the prefix "no" is the longest prefix that has a matching valid suffix (namely "on") at the end.
If a string is a palindrome (i.e. if the string is the same as its reverse), then the prefix is the whole string itself. For example, if input is "civic", then the longest prefix is "civic" itself. If the string has no valid prefix, print 0. For example, if the input string is "bird", there is a mismatch at the first character itself and there is no valid prefix.
The symbols in the string will only be from the set {A-Z,a-z}. The symbols are case sensitive. i.e. ‘A’ and ‘a’ are considered to be different.
You are provided with a function called printChars ( ) that prints a character array from the starting position pointed to by p to the ending position pointed to by q. If p is NULL, it prints 0.
The prototype of the function is:
void printChars(char *p, char *q);
You do not have to write the program for printChars ( ) function. This function is automatically added at the end of the code segment that you write.
You can use string functions in the library if you desire to.
INPUT:
Input is a string of length N composed of symbols only from the set {A-Za,z}.
OUTPUT:
The longest prefix that has a corresponding matching suffix and 0 if such a prefix does not exist.
Implementation:
If a string is a palindrome (i.e. if the string is the same as its reverse), then the prefix is the whole string itself. For example, if input is "civic", then the longest prefix is "civic" itself. If the string has no valid prefix, print 0. For example, if the input string is "bird", there is a mismatch at the first character itself and there is no valid prefix.
The symbols in the string will only be from the set {A-Z,a-z}. The symbols are case sensitive. i.e. ‘A’ and ‘a’ are considered to be different.
You are provided with a function called printChars ( ) that prints a character array from the starting position pointed to by p to the ending position pointed to by q. If p is NULL, it prints 0.
The prototype of the function is:
void printChars(char *p, char *q);
You do not have to write the program for printChars ( ) function. This function is automatically added at the end of the code segment that you write.
You can use string functions in the library if you desire to.
INPUT:
Input is a string of length N composed of symbols only from the set {A-Za,z}.
OUTPUT:
The longest prefix that has a corresponding matching suffix and 0 if such a prefix does not exist.
Implementation:
#include<stdio.h> #include<string.h> #include<stdlib.h> char str[100]; /* character array to store the string */ void printChars(char *p, char *q){ if(p == NULL) printf("0"); else{ char* itr = p; while(itr <= q){ printf("%c",*itr); itr++; } } } int length(char *s) { int len=0; while(*s) len++,s++; return len; } int main(){ int n; int beg,end; int flag=1; char *s; s=(char*)malloc(sizeof(char)*101); scanf("%s",s); beg = 0; end = length(s)-1; if(beg==end) printf("%c",s[beg]); else { while(beg<end) { if(s[beg]==s[end]) printf("%c",s[beg]),flag=0,beg++,end--; else break; } if(flag) printf("0"); } return 0; }