[英]using strncmp c-style string function
我有一个字符串,我试图找出它是否是另一个单词的子字符串。
例如(伪代码)
say I have string "pp"
and I want to compare it (using strncmp) to
happy
apples
pizza
and if it finds a match it'll replace the "pp" with "xx"
changing the words to
haxxles
axxles
pizza
使用strncmp可以吗?
不是直接使用strncmp
,而是可以使用strstr
:
char s1[] = "happy";
char *pos = strstr(s1, "pp");
if(pos != NULL)
memcpy(pos, "xx", 2);
仅当搜索和替换字符串的长度相同时,此方法才有效。 如果不是,则必须使用memmove
并可能分配更大的字符串来存储结果。
不适用于strncmp。 您需要strstrie
char happy = "happy";
char *s = strstr(happy, "pp");
if (s) memcpy(s, "xx", 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.