#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = "asdfgdfv";
char arr2[7] = "zxcvbn";
strncpy(arr1, arr2, 4);
printf("str0=%s\n", arr1);
char* str1 = strncpy(arr1, arr2, 4);
printf("str1=%s\n", arr1);
char* str2 = strncpy(arr1, arr2, 6);
printf("str2=%s\n", arr1);
char* str3 = strncpy(arr1, arr2, 7);
printf("str3=%s\n", arr1);
char* str4 = strncpy(arr2, arr2, 6);
printf("str4=%s\n", arr2);
return 0;
}
/* STRNCPY.C */
#include <string.h>
#include <stdio.h>
void main(void)
{
char string[100] = "Cats are nice usually";
printf("Before: %s\n", string);
strncpy(string, "Dogs", 4);
strncpy(string + 9, "mean", 4);
printf("After: %s\n", string);
}
?使用时需要包含头文件<string.h>
? 将源字符串的前num个字符复制到目标。如果源C字符串的末尾在复制num个字符之前找到'\0',剩下自动记0,直到总共写入了num个字符。
?拷贝num个字符从源字符串到目标空间。
?如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
#include <stdio.h>
#include <assert.h>
char* my_strncpy(char* dest, char* src, int count)
{
assert(dest && src);//注意 断言指针不是空指针
char* ret = dest;
while (count--)
{
*dest++ = *src++;
}
return ret;
}
int main()
{
char string[100] = "Cats are nice usually";
printf("Before: %s\n", string);
my_strncpy(string, "Dogs", 4);
my_strncpy(string + 9, "mean", 4);
printf("After: %s\n", string);
return 0;
}
#include <stdio.h>
#include <string.h>
//strncat
//附加字符
int main()
{
char string1[100] = "You and I";
strncat(string1, " together", 9);
printf("%s\n", string1);
char string2[100] = "You and I";
strncat(string2, " study together", 6);
printf("%s\n", string2);
return 0;
}
/* STRNCAT.C */
#include <string.h>
#include <stdio.h>
void main( void )
{
char string[80] = "This is the initial string!";
char suffix[] = " extra text to add to the string...";
/* Combine strings with no more than 19 characters of suffix: */
/* 组合后缀不超过19个字符的字符串:*/
printf( "Before: %s\n", string );
strncat( string, suffix, 19 );
printf( "After: %s\n", string );
}
?使用时需要包含头文件<string.h>
? 将源的前num个字符追加到目标,再加上一个终止的null字符。
(将来源指向字符串的前号码个字符追加到目的地指向的字符串末尾,再追加一个 \0字 符)。
? 如果源中的C字符串的长度小于num,则只有终止之前的内容空字符被复制。(如果来源指向的字符串的长度小于号码的时候,只会将字符串中到\0的内容追加到目的地指向的字符串末尾)。
#include <stdio.h>
#include <assert.h>
char* my_strncat(char* dest, char* src, int count)
{
assert(dest && src);
char* ret = dest;
while (*dest)
{
dest++;
}
while (count--)
{
*dest++ = *src++;
}
return ret;
}
int main()
{
char string[100] = "You and I";
char suffix[] = " in the rain";
printf("Before: %s\n",string);
my_strncat(string, suffix, 12);
printf("After: %s\n", string);
return 0;
}
//strncmp
//作用是 比较字符串的大小
#include <stdio.h>
#include <string.h>
int main()
{
char string1[] = "six";
char string2[] = "seven";
int len1 = strncmp(string1, string2, 3);
printf("%d\n", len1);
char string3[] = "six";
char string4[] = "seven";
int len2 = strncmp(string4, string3, 5);
printf("%d\n", len2);
return 0;
}
/* STRNCMP.C */
#include <string.h>
#include <stdio.h>
char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown fox jumps over the lazy dog";
void main(void)
{
char tmp[20];
int result;
printf("Compare strings:\n\t\t%s\n\t\t%s\n\n", string1, string2);
printf("Function:\tstrncmp (first 10 characters only)\n");
result = strncmp(string1, string2, 10);//比较两个字符串的大小,考虑大小写
if (result > 0)
strcpy(tmp, "greater than");
else if (result < 0)
strcpy(tmp, "less than");
else
strcpy(tmp, "equal to");
printf("Result:\t\tString 1 is %s string 2\n\n", tmp);
printf("Function:\tstrnicmp _strnicmp (first 10 characters only)\n");
result = _strnicmp(string1, string2, 10);//比较两个字符串的大小,不考虑大小写
if (result > 0)
strcpy(tmp, "greater than");
else if (result < 0)
strcpy(tmp, "less than");
else
strcpy(tmp, "equal to");
printf("Result:\t\tString 1 is %s string 2\n\n", tmp);
}
? 使用时,需要包含头文件<string.h>
? 比较字符串中对应的字符时,需要考虑到大小写(ASCII码值)
?返回值的规定:
?对于比较的字符有大小限制,但比较出现结果后,就会提前结束
#include <stdio.h>
#include <assert.h>
#include <string.h>//strcpy 需要的头文件
int my_strncmp(const char* s1, const char* s2, int count)
{
assert(s1 && s2);
while (count--)
{
if(*s1 == *s2)
{
if (*s1 == '\0')
return 0;
}
if (*s1 > *s2)
return 1;
else if(*s1 < *s2)
return -1;
s1++;
s2++;
}
}
int main()
{
char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown fox jumps over the lazy dog";
printf("Compare strings:\n\t\t%s\n\t\t%s\n\n", string1, string2);
char tmp[100];
int result = my_strncmp(string1, string2, 10);
if (result > 0)
strcpy(tmp, "greater than");
else if (result < 0)
strcpy(tmp, "less than");
else
strcpy(tmp, "equal than");
printf("Result:\n\tString1 is %s string2\n",tmp);
return 0;
}