Palindrome String C++ Suggestion for optimization

algorithm
c
c-plus-plus
palindrome
string

#1

It’s my code to find palindrome in string ,But it ain’t a good piece.How can I optimize this code or is there any efficient algorithm for this type of problems.(Palindrome in Strings)…And I think for some cases it is also not giving me correct output.Thanks for the help

//Have to work on it!!!!!!!!!!!!!!!!!!!!!//
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char input[100];
    char modified[100]="";  //This ll be modified form only with alphabets
   cout<<"Enter text:

";
gets(input);

    int t=strlen(input);
int s=0;
    for(int e=0;e<t;e++)
    {
        if(islower(input[e]))
        {
            modified[s] =input[e];
            s++;
        }
    }
printf("

The modified version of the text is:");
puts(modified);

int l=strlen(modified);
    for(int a=0;a<(l-1);a++)
        for(int b=a+1;b<l;b++)
    {
        if(modified[a]==modified**)
        {
            if((b-a)%2==0) //even
            {
                int x=(a+b)/2;
                int countt=0;
                for(int p=a,q=b;p<x && q>x;p++,q--)
                {
                    if(modified[p]==modified[q])
                        countt++;

                    else if(modified[p]!=modified[q])
                        break;

                    if(countt==(b-a)/2)
                    {
                        cout<<"Palindrome:";
                        for(int i=a;i<=b;i++)
                        {
                            putchar(modified*);
                        }
                        cout<<endl;
                    }
                }
            }

            else if((b-a)%2!=0) //odd
            {
                int y=(a+b+1)/2;
                int z=(a+b-1)/2;
                int count2=0;
                for(int w=a,r=b;w<y && r>z ;w++,r--)
                {
                                        if(modified[w]==modified[r])
                        count2++;

                    else if(modified[w]!=modified[r])
                        break;

                    if(count2==(b-a+1)/2)
                    {
                        cout<<"Palindrome:";
                        for(int o=a;o<=b;o++)
                        {
                            putchar(modified[o]);
                        }
                        cout<<endl;
                    }
                }
            }
        }
    }
}

#2

this code might solve your problem

#include"stdio.h"
#include"string"
#inclide "iostream"
using namespace std;


int main(){
	string s;
	char x;
	while(cin) s.push_back(cin.get());	
   	cout<<"You entered "<<s<<endl;
	char news[s.length()];
	for(int i=0;i<s.length();i++){
		if(s*>='A'&&s*<='Z')
			news*=s*+32; //to lowercase
		else news*=s*;
	}
    cout<<"modified string is"<<endl;
	for(int i=0;i<s.length();i++)
		cout<<news*;
    cout<<endl;
    cout<<"palindromes in the string are"<<endl;
	for(int i=1;i<s.length();i++){
		int fwd=i+1,bwd=i-1;
		while(news*==news[fwd])fwd++;
		while(news*==news[bwd])bwd--;
            //include all the characters which are same as central character, in either side of center character

		while(news[bwd]==news[fwd] && bwd>=0 && fwd<s.length()){
			cout<<s.substr(bwd,fwd-bwd+1)<<endl;
			j++;k--;
		}	
	}
}

in each iteration (for loop) consider chosen character as central character of palindrome, and characters on either side of the central character are checked for being palindrome. This algorithm works for string which have palindromes of even number of characters.