msg 裏的 text,放入 if()中判斷控制?
例如 : if( msg!= text){ printf ...; }
答: 可使用 strcmp , stricmp 解決
如果大小敏感,用strcmp
如果不敏感,用stricmp
這是標準C字符串函數
使用方法可以百度
程序基本如下:
############################################################
if
(
strcmp
(msg,
"text"
) != 0)
printf
(
"不相等"
);
不忽略大小寫
strcmp ,strncmp
忽略大小寫
windows平臺:
函數:stricmp(char *str1, char *str2), strnicmp(char *str1, char *str2, size_t n).
Linux平臺:
函數:strcasecmp(char *str1, char *str2), strncasecmp(char *str1, char *str2, size_t n)
############################################################
字符串比較需要使用字符串比較函數strcmp,不能直接用==來判斷。strcmp返回0表示相同,否則不相同
############################################################
建議帖主通讀include\string.h中的內容。
############################################################
字符串比較操作
if
( (
strcmp
(msg,
"text"
) == 0 )
// 表示相等
字符串拷貝操作
(
strcpy
(msg,
"text"
);
//拷貝操作
使用strcmp或者strncmp都可以,第二個函數能夠精確到比較字符的個數。
############################################################
另外補充下返回值爲0表示相等
############################################################
/*
thanks all super so much
感謝各大神人肋力
http://bbs.csdn.net/topics/390583837
*/