[ create a new paste ] login | about

Link: http://codepad.org/J3yl9MnC    [ raw code | output | fork ]

C++, pasted on Apr 10:
int main()
{
    char inputname[50] = "aaabbaa";
    char match[50];
    char temp[50];
    char revtemp[50];
    int length = 0;
    int currentpalindromelength = 0;
    int maxpalindromelength = 0;
    int palstart=0, palend=0;
    int i,j;
    for(i = 0;inputname[i] !='\0'; i++)
    {
        length++;
    }
    printf("length is %d\n", length);
    for(int ipos = 0; ipos  < length; ipos++)
    {
        for(j = ipos+1; j < length; j++)
        {
            int startpos=0;
            for(int k = ipos; k < j; k++)
            {
                temp[startpos++] = inputname[k];
            }
            temp[startpos] = '\0';
            printf("string to compare %s \n", temp);
            startpos=0;
            for(int z = j-1; z >=0; z--)
            {
                revtemp[startpos++] = inputname[z];
            }
            revtemp[startpos] = '\0';
            printf("reverse string to compare %s\n", revtemp);
            int result = strcmp(temp, revtemp);
            if (result == 0)
            {
                    printf("Both strings are equal \n");
                    currentpalindromelength = 0;
                    for(int iCur = 0;temp[iCur] !='\0'; iCur++)
                    {
                        currentpalindromelength ++;
                    }
                    if(currentpalindromelength > maxpalindromelength)
                    {
                        maxpalindromelength = currentpalindromelength;
                        palstart = ipos;
                        palend =j;
                    }
            }
            else
            {
                  printf("\nBoth strings are not equal \n");
           }
        }
    }
    printf("max palindrome length is %d",maxpalindromelength);
    printf("start pos is %d\n",palstart);
    printf("end pos is %d\n",palend);
    for(int k = palstart; k < palend; k++)
    {
         printf("%c",inputname[k]);
    }
    printf("\n");
    return 0;
}


Output:
length is 7
string to compare a 
reverse string to compare a
Both strings are equal 
string to compare aa 
reverse string to compare aa
Both strings are equal 
string to compare aaa 
reverse string to compare aaa
Both strings are equal 
string to compare aaab 
reverse string to compare baaa

Both strings are not equal 
string to compare aaabb 
reverse string to compare bbaaa

Both strings are not equal 
string to compare aaabba 
reverse string to compare abbaaa

Both strings are not equal 
string to compare a 
reverse string to compare aa

Both strings are not equal 
string to compare aa 
reverse string to compare aaa

Both strings are not equal 
string to compare aab 
reverse string to compare baaa

Both strings are not equal 
string to compare aabb 
reverse string to compare bbaaa

Both strings are not equal 
string to compare aabba 
reverse string to compare abbaaa

Both strings are not equal 
string to compare a 
reverse string to compare aaa

Both strings are not equal 
string to compare ab 
reverse string to compare baaa

Both strings are not equal 
string to compare abb 
reverse string to compare bbaaa

Both strings are not equal 
string to compare abba 
reverse string to compare abbaaa

Both strings are not equal 
string to compare b 
reverse string to compare baaa

Both strings are not equal 
string to compare bb 
reverse string to compare bbaaa

Both strings are not equal 
string to compare bba 
reverse string to compare abbaaa

Both strings are not equal 
string to compare b 
reverse string to compare bbaaa

Both strings are not equal 
string to compare ba 
reverse string to compare abbaaa

Both strings are not equal 
string to compare a 
reverse string to compare abbaaa

Both strings are not equal 
max palindrome length is 3start pos is 0
end pos is 3
aaa


Create a new paste based on this one


Comments: