- #include <stdio.h>
- #include <string.h>
- #define STR_SIZE 100
- void getLongestSubString( char* pString, char *buff)
- {
- if( pString == NULL )
- {
- return;
- }
- char temp[STR_SIZE] = {0};
- int subLength = 0;
- int first = 0;
- int last = strlen(pString) - 1;
- int i=0, j=0;
- for (first=0; first<strlen(pString)-1; first++)
- {
- for (last=strlen(pString)-1; last>first; last--)
- {
- if (pString[first] != pString[last])
- {
- continue;
- }
- i = first + 1;
- j = last - 1;
- while (i<j)
- {
- if (pString[i] != pString[j])
- {
- break;
- }
- i++;
- j--;
- }
- if ((i==j)||(i>j))
- {
- memcpy(temp, &pString[first], (&pString[last]-&pString[first]+1));
- if (strlen(temp) > subLength)
- {
- subLength = strlen(temp);
- memset(buff, 0, sizeof(buff));
- strcpy(buff, temp);
- }
- memset(temp, 0, sizeof(temp));
- }
- }
- }
- }
- int main(){
- char str[STR_SIZE] = {0};
- char buff[STR_SIZE] = {0};
- scanf("%s",str);
- getLongestSubString(str, buff);
- if (strlen(buff) == 0)
- {
- buff[0] = str[0];
- }
- printf("%s\\n", buff);
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/180220148729.html
来源: http://www.codesnippet.cn/detail/180220148729.html