字符串操作 c语言,C语言字符串操作(示例代码)
C語言字符串操作函數(shù)
1.strlen
strlen用于求一個C風(fēng)格字符串的長度,函數(shù)原型為
#include size_t strlen(const char *s);
返回值為字符串的長度,當(dāng)遇到‘\0‘時,認(rèn)為字符串結(jié)束,‘\0‘不算入長度中。
#include #include
int main(void)
{char *str = "hello,world";int count =strlen(str);
printf("%d\n",count);return 0;
}
結(jié)果測試:11
測試結(jié)果符合預(yù)期,下面一三種方式實(shí)現(xiàn)自己的strlen函數(shù)。
size_t my_strlen1(const char *str)
{int count=0;
assert(str);//while(str[count++]);//return count-1;
while(*str++)
count++;returncount;
}
size_t my_strlen2(const char *str)
{
assert(str);if(*str == ‘\0‘) return 0;else return my_strlen2(++str)+1;
}
size_t my_strlen3(const char *str)
{char *headstr =str;
assert(str);while(*++str);return str -headstr;
}
第一種使用一次遍歷字符串,當(dāng)字符不為‘\0‘時,count++;
第二種使用遞歸
第三種使用指針的剪發(fā),用指向最后一個字符的指針減去指向首字符的指針。
2.strnlen
strnlen的原型為
#include size_t strnlen(const char *s, size_t maxlen);
strnlen的功能相當(dāng)于一個計(jì)數(shù)器,函數(shù)從s的開頭往下數(shù),直到遇見‘\0‘或者計(jì)數(shù)器達(dá)到maxlne的值時,停止計(jì)數(shù)。
其用途主要在于網(wǎng)絡(luò)圖形中,字節(jié)流一般是沒有‘\0‘的,所以,使用strlen一般會有問題,所有引入strnlen.
3.strcpy
strcpy的原型為
#include
char *strcpy(char *dest, const char *src);
strcpy的作用是將src開始的字符串(包括‘\0‘)復(fù)制到以dest為起始點(diǎn)的地址空間,返回值為指向dest的指針。
#include #include
intmain()
{char dest[100];char *src = "hello,world";
strcpy(dest,src);
printf("%s\n",dest);return 0;
}
測試結(jié)果:hello,world
符合預(yù)期
實(shí)現(xiàn)自己的strcpy
#include #include
char *my_strcpy(char *dest,const char *src)
{
assert(dest);
assert(src);while (*dest++ = *src++);returndest;
}
4.strncpy
strncpy的原型是
#include
char *strncpy(char *dest, const char *src, size_t n);
該函數(shù)同樣字符串拷貝函數(shù),與strcpy不同的是,多了一個參數(shù)n。函數(shù)的作用是將src所指向的字符串中前n個字符復(fù)制到dest中。
5.strcmp
#include
int strcmp(const char *s1, const char *s2);
strcmp用來比較字符串s1和s2的大小,比較方式為字典式比較,比較標(biāo)準(zhǔn)為字符的ASCII值,比如"erfas"和"eradfasa";
逐個比較前兩個字符相等,第三個字符‘f’大于‘a(chǎn)‘,所以第一個字符串大。
當(dāng)s1 大于 s2時,返回正數(shù);
當(dāng)s1 等于 s2時,返回0;
當(dāng)s1 小于 s2時,返回負(fù)數(shù);
#include #include
intmain()
{char *s1 = "hello wolrd";char *s2 = "change world";int count =strcmp(s1,s2);
printf("%d\n",count);return 0;
}
這段代碼在vs2010下結(jié)果為1,在gcc下測試結(jié)果為5
C語言標(biāo)準(zhǔn)只規(guī)定當(dāng)s1 大于 s2時,返回值為正數(shù),vs一律返回1,而gcc返回ASCII值的差,所以為了程序的
可移植性,不要寫出
strcmp(s1,s2) == 1
這樣的代碼,建議這樣寫
strcmp(s1,s2) > 0
下面我們嘗試實(shí)現(xiàn)自己的strcmp
#include #include
int my_strcmp(const char *s1,const char *s2)
{
assert(s1);
assert(s2);while((*s1) == (*s2))
{if((*s1) == ‘\0‘)return 0;
s1++;
s2++;
}if((*s1) - (*s2) > 0) return 1;else return -1;
}
6.strncmp
strncmp的原型為
#include
int strncmp(const char *s1, const char *s2, size_t n);
該函數(shù)基本與strcmp差不多,但是只比較兩個字符串的前n個字符。
7.strstr
strstr的原型為
#include
char *strstr(const char *haystack, const char *needle);
判斷needle是否是haystack的子串,如果是,返回needle在haystack中首次出現(xiàn)的地址,否則,返回NULL
#include #include
intmain()
{char *s1 = "hello,world";char *s2 = "llo,w";char *s3 =strstr(s1,s2);
printf("%s\n",s3);return 0;
}
測試結(jié)果為:
llo,world
結(jié)果符合預(yù)期
8.memset
memset的原型為
#include
void *memset(void *s, int c, size_t n);
memset的作用是將s所指向的內(nèi)存中的前n個字節(jié)全部置為字符c的ASCII值。
#include #include
intmain()
{char s[100];
memset((char *)s,‘c‘,10);
s[10] = ‘\0‘;
printf("%s\n",s);return 0;
}
測試結(jié)果:cccccccccc
9.memcpy
memcpy
#include
void *memcpy(void *dest, const void *src, size_t n);
將src的數(shù)據(jù)復(fù)制n個到dest
#include #include
intmain()
{char src[100] = "hello,world";char dest[100];
memcpy(dest,src,11);
printf("%s\n",dest);return 0;
}
測試結(jié)果:hello,world
符合預(yù)期
memcpy不考慮內(nèi)存重疊
10.memmove
memmove的原型為
#include
void *memmove(void *dest, const void *src, size_t n);
memmove與memcpy相似,但可以處理內(nèi)存重疊的問題。
總結(jié)
以上是生活随笔為你收集整理的字符串操作 c语言,C语言字符串操作(示例代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 7 pxe,CentOS 7
- 下一篇: c语言程序设计和数据结构,C语言程序设计