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.
Use function : void printChars(char *p, char *q);
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.
Use function : void printChars(char *p, char *q);
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.
CONSTRAINTS:
The inputs will satisfy the following properties. It is not necessary to validate the inputs.
1<=N<=99
#include<stdio.h>
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.
CONSTRAINTS:
The inputs will satisfy the following properties. It is not necessary to validate the inputs.
1<=N<=99
#include<stdio.h>
#include<string.h>
void printChars(char *p,char *q);//function prototype
int main()
{
char a[100],*p,*q;
int x=0,y,i=0,f=0;
scanf("%s",a);
for(i=0;i<strlen(a);i++)
{
y=a[i];
if( (y<65) || ( y>90 && y<97) || (y>122) )
x++;
}
i=0;
if((a[0]==a[strlen(a)-1]) && (x==0))
{
p=&a[0];
while((a[i]==a[strlen(a)-i-1])&&(f<strlen(a)))
{
q=&a[i];
i++;
f++;
}
}
else
p=NULL;
printChars(p,q);
return 0;
}
void printChars(char *p, char *q)
{
if (p==NULL)
{
printf("0");
}
else
{
while(p <= q)
{
printf("%c",*p);
p++;
}
}
}
No comments:
Post a Comment