需求开发中经常会用到很字符串操作的函数,现整理常用函数如下:
函数名: strcpy
功 能: 拷贝一个字符串到另一个
用 法: char *strcpy(char *destin, char *source);
示例如下:
<span style="font-size:24px;">#include <stdio.h> #include <string.h> int main() { char desstr[10]; char *srcstr = "abcdefg"; strcpy(desstr,srcstr); printf("%s\n",desstr); } </span>
<pre name="code" class="cpp">#include <stdio.h> #include <string.h> int main() { char temstr [50] = {0}; char *desstr = "123456"; char *srcstr="789"; char *temp = NULL; strcpy(temstr,desstr); strcat(temstr,srcstr); temp = strchr(temstr,'7'); printf("%s",temp); }
说明:返回首次出现c的位置的指针,返回的地址是被查找字符串指针开始的第一个与Val相同字符的指针,如果s中不存在c则返回NULL。
程序例
#include <string.h> #include <stdio.h> int main() { char temstr [50] = {0}; char *desstr = "123456"; char *srcstr="789"; strcpy(temstr,desstr); strcat(temstr,srcstr); printf("%s",temstr); }函数名: strcmp
/* 当s1<s2时,返回为负数 当s1=s2时,返回值= 0 当s1>s2时,返回正数 */ #include <stdio.h> #include <string.h> int main() { char string[20]; char str[3][20]; int i; for(i=0;i<3;i++) gets(str[i]); if(strcmp(str[0],str[1])>0) strcpy(string,str[0]); else strcpy(string,str[1]); if(strcmp(str[2],string)>0) strcpy(string,str[2]); printf("\nThe largest string is%s\n",string); return0; }
#include <stdio.h> #include <string.h> int main() { char *desstr = "holle ,hi 123"; char *srcstr=","; int relust = 0; relust = strcspn(desstr,srcstr); printf("%d",relust); }
#include <stdio.h> #include <string.h> #include <alloc.h> int main(void) { char *dup_str, *string = "abcde"; dup_str = strdup(string); printf("%sn", dup_str); free(dup_str); return 0; }
#include <string.h> #include <stdio.h> int main(void) { char *buf1 = "BBB", *buf2 = "bbb"; int ptr; ptr = stricmp(buf2, buf1); if (ptr > 0) printf("buffer 2 is greater than buffer 1n"); if (ptr < 0) printf("buffer 2 is less than buffer 1n"); if (ptr == 0) printf("buffer 2 equals buffer 1n"); return 0; }
指向错误信息的指针(即:错误的描述字符串)。
程序例:
#include<stdio.h> #include<string.h> #include<errno.h> #include<stdlib.h> int main(void) { FILE*fp; externinterrno; char*message; if(NULL==(fp=fopen("/dev/dsp","r+"))) { printf("errno=%d\n",errno); message=strerror(errno); printf("Mesg:%s\n",message); } exit(0); } /* 输出: error=2 Mesg:No such file or direcory */
/* strncmp函数是指定比较size个字符。也就是说,如果字符串s1与s2的前size个字符相同,函数返回值为0。此函数功能即比较字符串str1和str2的前maxlen个字符。如果前maxlen字节完全相等,返回值就=0;在前maxlen字节比较过程中,如果出现str1[n]与str2[n]不等,则依次比较str1和str2的前n位,设i(i<n)为两字符串首次的不同位,则返回(str1[i]-str2[i])。 */ #include<string.h> #include<stdio.h> int main(void) { char *buf1="aaabbb",*buf2="bbbccc",*buf3="ccc"; int ptr; ptr=strncmp(buf2,buf1,3); if(ptr>0) printf("buffer2 is greater than buffer1\n"); elseif(ptr<0) printf("buffer2 is less than buffer1\n"); ptr=strncmp(buf2,buf3,3); if(ptr>0) printf("buffer2 is greater than buffer3\n"); elseif(ptr<0) printf("buffer2 is less than buffer3\n"); return(0); }
可对比strcpy方法使用;
程序例:
#include <stdio.h> #include <string.h> int main(void) { char string[10]; char *str1 = "abcdefghi"; strncpy(string, str1, 3); /*string 中必须保证有足够的空间存储被拷贝进去的字符串*/ string[3] = ''; printf("%sn", string); return 0; }
程序例:
#include<stdio.h> #include<string.h> int main(void) { char string[55]="abcdefghijklmnopqrstuvwxyz"; char letter='x'; printf("stringbeforestrnset:%s\n",string); strnset(string,letter,13); printf("stringafterstrnset:%s\n",string); return 0; }
#include <stdio.h> #include <string.h> int main(void) { char *string1 = "abcdefghijklmnopqrstuvwxyz"; char *string2 = "onm"; char *ptr; ptr = strpbrk(string1, string2); if (ptr) printf("strpbrk found first character: %cn", *ptr); else printf("strpbrk didn't find character in setn"); return 0; }
#include <stdio.h> #include <string.h> int main(void) { char *str1 = "Borland International", *str2 = "nation", *ptr; ptr = strstr(str1, str2); printf("The substring is: %sn", ptr); return 0; }
函数名: strtod
功 能: 将字符串转换为double型值
用 法: double strtod(char *str, char **endptr);
程序例:
/* strtod()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时('\0')才结束转换,并将结果返回。若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分。如123.456或123e-2。 */ #include<stdlib.h> #include<stdio.h> void main() { char *endptr; char a[] = "12345.6789"; char b[] = "1234.567qwer"; char c[] = "-232.23e4"; printf( "a=%lf\n", strtod(a,NULL) ); printf( "b=%lf\n", strtod(b,&endptr) ); printf( "endptr=%s\n", endptr ); printf( "c=%lf\n", strtod(c,NULL) ); } /*执行: 1 2 3 4 a=12345.678900 b=1234.567000 endptr=qwer c=-2322300.000000 */函数名: strtol
#include <stdlib.h> #include <stdio.h> int main(void) { char *string = "87654321", *endptr; long lnumber; /* strtol converts string to long integer */ lnumber = strtol(string, &endptr, 10); printf("string = %s long = %ldn", string, lnumber); return 0; }
#include <stdio.h> #include <string.h> int main(void) { char *string = "abcdefghijklmnopqrstuvwxyz", *ptr; /* converts string to upper case characters */ ptr = strupr(string); printf("%sn", ptr); return 0; }函数名: swab
#include <stdlib.h> #include <stdio.h> #include <string.h> char source[15] = "rFna koBlrna d"; char target[15]; int main(void) { swab(source, target, strlen(source)); printf("This is target: %sn", target); return 0; }
当然还有很多欢迎补充!
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/knight1840/article/details/47377895