[英]How to compare two strings in C ignoring the new line character?
我正在研究C編程,我知道在C中strcmp
用於比較兩個字符串,但是比較中總是包含換行符,我想忽略它。 我知道可以刪除換行符然后進行比較,但是是否有任何功能會自動忽略換行符?
這是打開函數代碼更容易的情況之一。 我已經對其進行了嚴格的相等性測試(即不是按字母順序排列),盡管那是一個相對容易的更改。
嘗試一下,這實際上應該執行原始發問者的要求:
/* Compare two strings s1 and s2, assuming s1 is terminated
* by \n or a NULL, and s2 is terminated by a NULL. A match
* returns 0, a non-match returns 1.
*/
int
strcmpst1nl (const char * s1, const char * s2)
{
char s1c;
do
{
s1c = *s1;
if (s1c == '\n')
s1c = 0;
if (s1c != *s2)
return 1;
s1++;
s2++;
} while (s1c); /* already checked *s2 is equal */
return 0;
}
有趣的是, for
循環而言,這並不是特別優雅。 更優雅的答案表示贊賞。
比較兩個字符串可以用\\n
或NULL
終止(不完全是您所要求的)的更通用的例程是:
/* Compare two strings s1 and s2, assuming either is
* terminated by \n or a NULL, A match
* returns 0, a non-match returns 1.
*/
int
strcmpnl (const char *s1, const char *s2)
{
char s1c;
char s2c;
do
{
s1c = *(s1++);
s2c = *(s2++);
if (s1c == '\n')
s1c = 0;
if (s2c == '\n')
s2c = 0;
if (s1c != s2c)
return 1;
}
while (s1c); /* already checked *s2 is equal */
return 0;
}
另一個效率較低的路由(假設s1以\\n
終止)是:
#include <string.h>
#include <strings.h>
int
strcmpst1nl2 (const char *s1, const char *s2)
{
int s1len, s2len;
s1len = strlen (s1);
s2len = strlen (s2);
/* check strings are equal length without \n */
if (s1len - 1 != s2len)
return 1;
/* we know s1len > 0, as s2len would be -1, so this is safe */
if (s1[s1len - 2] != '\n')
return 1;
return bcmp (s1, s2, s1len - 1);
}
首先,您應該編寫一個計算字符串長度的函數,在該函數中,標准終止符和結束符均終止該字符串。 為此,建議您同時檢查換行符和回車符( 在此處檢查)
然后, 根據前面的定義檢查兩個字符串的 長度是否相同 。
如果是,則進一步使用strncmp檢查字符串(並將找到的長度用作第三個參數)。
可以使用以下函數代替strcmp()
。 它執行相同的操作,但是忽略第二個字符串上的一個(可能)尾隨換行符。 它不會修改字符串。
int my_cmp(const char *str1, const char *str2)
{
int r,n;
/* Get length of str1, which is also the index of the 1st additional
* char in str2
*/
n = strlen(str1);
/* Compare the first n chars */
r = strncmp(str1,str2,n);
/* If 1st n chars match, make sure next char in str2 is either the null char
* or a newline followed by the null char. Otherwise, return -1 since str2 is
* longer (making str1 "less than" str2)
*/
if ( !r && str2[n] && (str2[n] != '\n' || str2[n+1]) )
return -1;
return r;
}
這是另一個忽略任一字符串中任何地方的所有換行符的字符串:
int my_cmp(const char *str1, const char *str2)
{
const unsigned char *a, *b;
/* loop over both strings */
for (a = str1, b = str2; ; a++, b++) {
while (*a == '\n') a++; /* skip newlines in str1 */
while (*b == '\n') b++; /* skip newlines in str2 */
/* If we reach the end of either string (or both), one of
* the if's below will terminate the loop */
/* return if we're at end of both strings */
if (!(*a || *b))
return 0;
/* positive return when str1's char is greater than str2's
* (or at end of str2) */
if (*a > *b)
return 1;
/* negative return when str1's char is less than str2's
* (or at end of str1) */
if (*a < *b)
return -1;
}
return 0;
}
當然,如果您只需要忽略單個尾隨的換行符,就很容易將其剝離(只需找到它並用'\\0'
覆蓋它)...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.