输出结果:s1lessthans2
int strncmp(char *str1, char *str2, intmaxlen);
当s1<s2时,返回为负数
当s1=s2时,返回=0
当s1>s2时,返回正数
#include<string.h>
#include<stdio.h>
int main()
{
char *buf1="aaabbb",*buf2="bbbccc",*buf3="ccc";
int ptr;
ptr=strncmp(buf2,buf1,3);
if(ptr>0)
printf("buffer2 is greater than buffer1\n");
else if(ptr<0)
printf("buffer2 is less than buffer1\n");
ptr=strncmp(buf2,buf3,3);
if(ptr>0)
printf("buffer2 is greater thanbuffer3\n");
else if(ptr<0)
printf("buffer2 is less than buffer3\n");
return 0;
}
输出结果:
buffer2 is greater than buffer1
buffer2 is less than buffer3
char *strtok(char s[], const char*delim);
分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
例如:strtok("abc,def,ghi",","),最后可以分割成为abc def ghi.
#include<string.h>
#include<stdio.h>
int main()
{
charinput[16]="abc,d,efg";
char *p;
p=strtok(input,",");
while(p)
{
printf("%s\n",p);
p=strtok(NULL,",");
}
return 0;
}
输出结果:abc
d
efg
void *memcpy(void *dest, const void *src,size_t n);
从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中
#include<stdio.h>
#include<string.h>
int main()
{
char s[]="GoldenGlobalView";
char d[20];
memcpy(d,s12,4);//从第13个字符(V)开始复制,连续复制4个字符
d[4]='\0';//memcpy(d,s14*sizeof(char),4*sizeof(char));也可
printf("%s\n",d);
return 0;
}
输出结果:View
倒置字符串
#include<stdio.h>
#include<string.h>
void reverse(char s[])
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-34960-11.html
发动一场战争