老實說,你並不需要一個字符串切割功能只是爲了子內檢查迴文:
/* start: Pointer to first character in the string to check.
* end: Pointer to one byte beyond the last character to check.
*
* Return:
* -1 if start >= end; this is considered an error
* 0 if the substring is not a palindrome
* 1 if the substring is a palindrome
*/
int
ispalin (const char *start, const char *end)
{
if (start >= end)
return -1;
for (; start < end; ++start)
if (*start != *--end)
return 0;
return 1;
}
有了這一點,你可以創建以下文件:
int
main()
{
const char *s = "madam";
/* i: index of first character in substring
* n: number of characters in substring
*/
size_t i, n;
size_t len = strlen (s);
for (i = 0; i < len; ++i)
{
for (n = 1; n <= len - i; ++n)
{
/* Start of substring. */
const char *start = s + i;
/* ispalin(s[i:i+n]) in Python */
switch (ispalin (start, start + n))
{
case -1:
fprintf (stderr, "error: %p >= %p\n", (void *) start, (void *) (start + n));
break;
case 0:
printf ("Not a palindrome: %.*s\n", (int) n, start);
break;
case 1:
printf ("Palindrome: %.*s\n", (int) n, start);
break;
} /* switch (ispalin) */
} /* for (n) */
} /* for (i) */
}
當然,如果你真的只想輸出字符串切片功能(因爲你在技術上不應該投size_t
到int
),你仍然希望能夠容易地格式化輸出,Paul Griffiths的答案應該足夠qui忒好了,或者你可以用我的或者strncpy
或非標準strlcpy
連一個,但他們都有自己的長處和短處:
/* dest must have
* 1 + min(strlen(src), n)
* bytes available and must not overlap with src.
*/
char *
strslice (char *dest, const char *src, size_t n)
{
char *destp = dest;
/* memcpy here would be ideal, but that would mean walking the string twice:
* once by calling strlen to determine the minimum number of bytes to copy
* and once for actually copying the substring.
*/
for (; n != 0 && *src != 0; --n)
*destp++ = *src++;
*destp = 0;
return dest;
}
strslice
實際工作像strncpy
和非標準strlcpy
組合,雖然有這三個函數之間的差異:
strlcpy
將削減被複制字符串短於在dest[n - 1]
添加空終止子,所以複製完全相同n
字節添加空TE之前rminator要求您將n + 1
作爲緩衝區大小。
strncpy
可能根本不會終止字符串,因此dest[n - 1]
等於src[n - 1]
,所以您需要自己添加空終止符以防萬一。如果n
大於src
字符串長度,則dest
將填充空終止符,直到寫入n
個字節。
strslice
將根據需要複製到n
字節,如strncpy
,並且將需要額外的空字符作爲空終止符,這意味着最大需要n+1
字節。它不會浪費時間寫入不必要的空終止符,如strncpy
所做的那樣。這可以被認爲是「輕量級strlcpy
」,其中n
的含義差別很小,並且可以在產生的字符串長度無關緊要的情況下使用。
如果需要,您也可以創建memslice
函數,這將允許嵌入的空字節,但它已經存在爲memcpy
。
如果您不想(或不能)更改源字符串,您需要製作每個子字符串的副本,例如如果你在Linux上使用[strndup(s + start,length)](http://linux.die.net/man/3/strndup)。你需要提取子字符串嗎?你能比較一下人物序列嗎? – Rup 2014-10-28 23:42:03
C不是我爲花哨的字符串處理選擇的語言。你將不得不手動索引所有的子串。 – 2014-10-28 23:44:22