top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Given a string, add some characters to the from of it so that it becomes a palindrome?

0 votes
100 views

Given a string, add some characters to the from of it so that it becomes a palindrome.
e.g.
1) If input is "abc" then "bcabc" should be returned.
2) input -> "ab" output -> "bab"
3) input -> "a" output -> "a"

posted Dec 16, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
#include <stdio.h>                                                                                  
int main()                                                                                          
{                                                                                                   
    char string[500];                                                                               
    int len;                                                                                        
    printf("Enter the input string\n");                                                             
    scanf("%s",string);                                                                             
    len = strlen(string);                                                                           
    char *outputstr = (char *)malloc(len -2);                                                       
    /** We can avoid this extra variable then we need one more integer to point                     
     * the end of the string , so both are same 4 bytes */                                          
    char *substr = ((char *)string) + (len - 1);                                                    
    len = 0;                                                                                        
    /** Instead of while and strcat we can make single loop but that will                           
     * modify the Source string , so made it two loop */                                           
    while(substr != string){                                                                        
                outputstr[len] = *substr;                                                                   
                substr--;                                                                                   
                len++;                                                                                      
    }                                                                                               
    outputstr[len] = '\0';                                                                          
    strcat(outputstr,string);                                                                       
    printf("Out put string %s\n",outputstr);                                                        

}
answer Dec 16, 2016 by Jaganathan
Similar Questions
+4 votes

Given a dictionary of strings and another string find out if the string is an exact match to some words in the dictionary or varies at most in only one place of some word of the dictionary?

+1 vote

Given a string and dictionary of words, form a word by removing minimum number of characters.
Characters can be removed in-order only.

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...