#include <stdio.h> #include <string.h> int is_palindrome(char* string, int start, int end) { int i = start, j = end;
while (start <= end) { if (string[start++] != string[end--]) return 0; } /* print */ printf("[%d,%d] ", i, j); while ( i <= j) printf("%c", string[i++]); printf("\n"); return 1; } int main(int argc, char** argv) { int i, j, k; int len, rescan; char demo[] = "madamimadam"; char *p; if (argc != 2) { printf("usage padin (string)\n"); return 0; } p = argv[1]; do { rescan = 0; len = strlen(p); for (i = 0; i < len && rescan == 0; i++) { for (j = i, k = len; j <= k; k--) if (is_palindrome(p, j, k)) { p += k - j + 1; rescan = 1; break; } } } while (rescan); return 0; } anders@ubuntu:~/c$ gcc palindrome.c -o palin anders@ubuntu:~/c$ ./palin helloworld [0,0] h [0,0] e [0,1] ll [0,2] owo [0,0] r [0,0] l [0,0] d anders@ubuntu:~/c$ ./palin madamimadam [0,10] madamimadam anders@ubuntu:~/c$ ./palin andersma [0,0] a [0,0] n [0,0] d [0,0] e [0,0] r [0,0] s [0,0] m [0,0] a On Tue, May 10, 2011 at 10:11 AM, oldman <fenghaungyuyi...@gmail.com> wrote: > I agree with Anders Ma's point,but in my opinion, using goto is risky in a > import interview > > On Tue, May 10, 2011 at 9:52 AM, Anders Ma <xuejiao...@gmail.com> wrote: >> >> sometimes we need goto, goto is not so evil. >> >> On Tue, May 10, 2011 at 2:49 AM, Manjeet Chayel >> <chayel.manj...@gmail.com> wrote: >> > Dont use goto... its not good to have it. >> > >> > On Mon, May 9, 2011 at 2:44 PM, Anders Ma <xuejiao...@gmail.com> wrote: >> >> >> >> #include <stdio.h> >> >> #include <string.h> >> >> >> >> int is_palindrome(char* string, int start, int end) >> >> { >> >> int i = start, j = end; >> >> >> >> while (start <= end) { >> >> if (string[start++] != string[end--]) >> >> return 0; >> >> } >> >> >> >> /* print */ >> >> printf("[%d,%d] ", i, j); >> >> while ( i <= j) >> >> printf("%c", string[i++]); >> >> printf("\n"); >> >> >> >> return 1; >> >> } >> >> >> >> int main(int argc, char** argv) >> >> { >> >> int i, j, k; >> >> int len; >> >> char *p; >> >> >> >> if (argc != 2) { >> >> printf("usage padin (string)\n"); >> >> return 0; >> >> } >> >> p = argv[1]; >> >> BEGIN: >> >> >> >> len = strlen(p); >> >> for (i = 0; i < len; i++) { >> >> for (j = i, k = len; j <= k; k--) >> >> if (is_palindrome(p, j, k)) { >> >> p += k - j + 1; >> >> goto BEGIN; >> >> } >> >> } >> >> >> >> return 0; >> >> } >> >> >> >> anders@ubuntu:~/c$ ./palin helloworld >> >> [0,0] h >> >> [0,0] e >> >> [0,1] ll >> >> [0,2] owo >> >> [0,0] r >> >> [0,0] l >> >> [0,0] d >> >> anders@ubuntu:~/c$ ./palin madamamadam >> >> [0,10] madamamadam >> >> anders@ubuntu:~/c$ >> >> >> >> >> >> >> >> On Fri, May 6, 2011 at 8:58 PM, sourabh jakhar >> >> <sourabhjak...@gmail.com> >> >> wrote: >> >> > >> >> > >> >> > On Fri, May 6, 2011 at 4:23 PM, sourabh jakhar >> >> > <sourabhjak...@gmail.com> >> >> > wrote: >> >> >> >> >> >> You are given a large string. You need to cut the string into chunks >> >> >> such >> >> >> that each substring that you get is a palindrome. Remember that each >> >> >> 1 >> >> >> length string is always a palindrome. You need to find the minimum >> >> >> number of >> >> >> cuts that you need to make such that each substring is a palindrome. >> >> >> >> >> >> -- >> >> >> SOURABH JAKHAR,(CSE)(3 year) >> >> >> ROOM NO 167 , >> >> >> TILAK,HOSTEL >> >> >> 'MNNIT ALLAHABAD >> >> >> >> >> >> The Law of Win says, "Let's not do it your way or my way; let's do >> >> >> it >> >> >> the >> >> >> best way." >> >> >> >> >> >> -- >> >> >> You received this message because you are subscribed to the Google >> >> >> Groups >> >> >> "Algorithm Geeks" group. >> >> >> To post to this group, send email to algogeeks@googlegroups.com. >> >> >> To unsubscribe from this group, send email to >> >> >> algogeeks+unsubscr...@googlegroups.com. >> >> >> For more options, visit this group at >> >> >> http://groups.google.com/group/algogeeks?hl=en. >> >> > >> >> > >> >> > >> >> > -- >> >> > SOURABH JAKHAR,(CSE)(3 year) >> >> > ROOM NO 167 , >> >> > TILAK,HOSTEL >> >> > 'MNNIT ALLAHABAD >> >> > >> >> > The Law of Win says, "Let's not do it your way or my way; let's do it >> >> > the >> >> > best way." >> >> > >> >> > -- >> >> > You received this message because you are subscribed to the Google >> >> > Groups >> >> > "Algorithm Geeks" group. >> >> > To post to this group, send email to algogeeks@googlegroups.com. >> >> > To unsubscribe from this group, send email to >> >> > algogeeks+unsubscr...@googlegroups.com. >> >> > For more options, visit this group at >> >> > http://groups.google.com/group/algogeeks?hl=en. >> >> > >> >> >> >> >> >> >> >> -- >> >> Regards >> >> Anders >> >> >> >> -- >> >> You received this message because you are subscribed to the Google >> >> Groups >> >> "Algorithm Geeks" group. >> >> To post to this group, send email to algogeeks@googlegroups.com. >> >> To unsubscribe from this group, send email to >> >> algogeeks+unsubscr...@googlegroups.com. >> >> For more options, visit this group at >> >> http://groups.google.com/group/algogeeks?hl=en. >> >> >> > >> > >> > >> > -- >> > Cheers!! >> > Manjeet Chayel >> > >> > -- >> > You received this message because you are subscribed to the Google >> > Groups >> > "Algorithm Geeks" group. >> > To post to this group, send email to algogeeks@googlegroups.com. >> > To unsubscribe from this group, send email to >> > algogeeks+unsubscr...@googlegroups.com. >> > For more options, visit this group at >> > http://groups.google.com/group/algogeeks?hl=en. >> > >> >> >> >> -- >> Regards >> Anders >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Regards Anders -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.