å½æ°å: strrchr
å è½: å¨ä¸²ä¸æ¥æ¾æå®å符çæåä¸ä¸ªåºç°
ç¨ æ³: char *strrchr(char *str, char c);
举ä¾ï¼
[cpp] view plain copy
char fullname="./lib/lib1.so";
char *ptr;
ptr = strrchr(fullname,'/');
printf("filename is %s",++ptr);
//è¿è¡ç»æï¼filename is lib1.so
å½æ°å: strchr
å è½: å¨ä¸²ä¸æ¥æ¾æå®å符ç第ä¸ä¸ªåºç°
ç¨ æ³: char *strchr(char *str, char c);
举ä¾ï¼
[cpp] view plain copy
char fullname="./lib/lib1.so";
char *ptr;
ptr = strrchr(fullname,'.');
printf("after strchr() is %s",++ptr);
//è¿è¡ç»æï¼after strchr() is /lib/lib1.so
å½æ°å: strtok
å è½: å¨ä¸²ä¸æ¥æ¾æå®å符ç第ä¸ä¸ªåºç°
ç¨ æ³: char *strtok(char *s, char *delim);
说æï¼
1.strtokå½æ°çå®è´¨ä¸çå¤çæ¯ï¼strtokå¨sä¸æ¥æ¾å
å«å¨delimä¸çå符并ç¨NULL(â/0â²)æ¥æ¿æ¢,ç´å°æ¾éæ´ä¸ªå符串ãè¿å¥è¯æ两å±å«ä¹ï¼ï¼1ï¼æ¯æ¬¡è°ç¨strtokå½æ°åªè½è·å¾ä¸ä¸ªåå²åä½ãï¼2ï¼è¦è·å¾ææçåå²åå
å¿
é¡»åå¤è°ç¨strtokå½æ°ã
2.strtokå½æ°ä»¥åçè°ç¨æ¶çéç¨NULLæ¥æ¿æ¢s.
3.å½¢ås(è¦åå²çå符串)对åºçåéåºç¨char s[]=ââ¦.âå½¢å¼ï¼èä¸è½ç¨char *s=ââ¦.âå½¢å¼ã
举ä¾ï¼
[cpp] view plain copy
void main()
{
char buf[]=âGolden Global Viewâ;
char* token = strtok( buf, â â);
while( token != NULL )
{
printf( â%s â, token );
token = strtok( NULL, â â);
}
return 0;
}
/*å
¶ç»æ为ï¼
Golden
Global
View
*/
å½æ°åï¼strncpy
åè½ï¼æsrcææç±NULLç»æçå符串çån个åèå¤å¶å°destææçæ°ç»ä¸
ç¨æ³ï¼char *strncpy(char *dest, char *src, int n);
说æï¼
å¦æsrcçån个åèä¸å«NULLå符ï¼åç»æä¸ä¼ä»¥NULLå符ç»æã
å¦æsrcçé¿åº¦å°äºn个åèï¼å以NULLå¡«å
destç´å°å¤å¶å®n个åèã
srcådestææå
ååºåä¸å¯ä»¥éå ä¸destå¿
é¡»æ足å¤ç空é´æ¥å®¹çº³srcçå符串ã
è¿åæådestçæéã
举ä¾ï¼
[c-sharp] view plain copy
#include <syslib.h>
#include <string.h>
main()
{
char buf[4];
char *s="abcdefg";
strncpy(buf,s,4);
printf("%s/n",buf);
return 0;
}
/*è¿è¡ç»æï¼
abcd
*/
å½æ°å: stpcpy
å è½: æ·è´ä¸ä¸ªå符串å°å¦ä¸ä¸ª
ç¨ æ³: char *stpcpy(char *destin, char *source);
举ä¾ï¼
[cpp] view plain copy
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
stpcpy(string, str1);
printf("%s/n", string);
return 0;
}
/*è¿è¡ç»æ
abcdefghi
*/
å½æ°å: strcat
å è½: å符串æ¼æ¥å½æ°
ç¨ æ³: char *strcat(char *destin, char *source);
举ä¾ï¼
[cpp] view plain copy
#include <string.h>
#include <stdio.h>
int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%s/n", destination);
return 0;
}
/*è¿è¡ç»æï¼
Borland C++
*/
å½æ°å: strcmp
å è½: 串æ¯è¾
ç¨ æ³: int strcmp(char *str1, char *str2);
çAsicç ï¼str1>str2ï¼è¿åå¼ > 0ï¼ä¸¤ä¸²ç¸çï¼è¿å0
举ä¾ï¼
[cpp] view plain copy
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb"ï¼
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1/n");
else if(ptr < 0)
printf("buffer 2 is less than buffer 1/n");
else
printf("buffer 2 is equal with buffer 1/n");
return 0;
}
/*è¿è¡ç»æï¼
buffer 2 is greater than buffer 1
*/
å½æ°å: strncmpi
å è½: å°ä¸ä¸ªä¸²ä¸çä¸é¨åä¸å¦ä¸ä¸ªä¸²æ¯è¾, ä¸ç®¡å¤§å°å
ç¨ æ³: int strncmpi(char *str1, char *str2, unsigned maxlen);
举ä¾ï¼
[cpp] view plain copy
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1/n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1/n");
if (ptr == 0)
printf("buffer 2 equals buffer 1/n");
return 0;
}
å½æ°å: strcspn
å è½: å¨ä¸²ä¸æ¥æ¾ç¬¬ä¸ä¸ªç»å®å符éå
容ç段
ç¨ æ³: int strcspn(char *str1, char *str2);
举ä¾ï¼
[cpp] view plain copy
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *string1 = "1234567890";
char *string2 = "747DC8";
int length;
length = strcspn(string1, string2);
printf("Character where strings intersect is at position %d/n", length);
return 0;
}
å½æ°å: strdup
å è½: å°ä¸²æ·è´å°æ°å»ºçä½ç½®å¤
ç¨ æ³: char *strdup(char *str);
举ä¾ï¼
[cpp] view plain copy
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *dup_str, *string = "abcde";
dup_str = strdup(string);
printf("%s/n", dup_str);
free(dup_str);
return 0;
}
å½æ°å: stricmp
å è½: 以大å°åä¸æææ¹å¼æ¯è¾ä¸¤ä¸ªä¸²
ç¨ æ³: int stricmp(char *str1, char *str2);
举ä¾ï¼
[cpp] view plain copy
#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 1/n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1/n");
if (ptr == 0)
printf("buffer 2 equals buffer 1/n");
return 0;
}
å½æ°å: strerror
å è½: è¿åæåé误信æ¯å符串çæé
ç¨ æ³: char *strerror(int errnum);
举ä¾ï¼
[cpp] view plain copy
#include <stdio.h>
#include <errno.h>
int main(void)
{
char *buffer;
buffer = strerror(errno);
printf("Error: %s/n", buffer);
return 0;
}
å½æ°å: strncmp
å è½: 串æ¯è¾
ç¨ æ³: int strncmp(char *str1, char *str2, int maxlen);
举ä¾ï¼
[cpp] view plain copy
#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("buffer 2 is greater than buffer 1/n");
else
printf("buffer 2 is less than buffer 1/n");
ptr = strncmp(buf2,buf3,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3/n");
else
printf("buffer 2 is less than buffer 3/n");
return(0);
}
å½æ°å: strncmpi
å è½: æ串ä¸çä¸é¨åä¸å¦ä¸ä¸²ä¸çä¸é¨åæ¯è¾, ä¸ç®¡å¤§å°å
ç¨ æ³: int strncmpi(char *str1, char *str2, int len);
举ä¾ï¼
[cpp] view plain copy
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;
ptr = strncmpi(buf2,buf1,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1/n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1/n");
if (ptr == 0)
printf("buffer 2 equals buffer 1/n");
return 0;
}
å½æ°å: strnset
å è½: å°ä¸ä¸ªä¸²ä¸çææå符é½è®¾ä¸ºæå®å符
ç¨ æ³: char *strnset(char *str, char ch, unsigned n);
举ä¾ï¼
[cpp] view plain copy
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';
printf("string before strnset: %s/n", string);
strnset(string, letter, 13);
printf("string after strnset: %s/n", string);
return 0;
}
å½æ°å: strpbrk
å è½: å¨ä¸²ä¸æ¥æ¾ç»å®å符éä¸çå符
ç¨ æ³: char *strpbrk(char *str1, char *str2);
举ä¾ï¼
[cpp] view plain copy
#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: %c/n", *ptr);
else
printf("strpbrk didn't find character in set/n");
return 0;
}
å½æ°å: strrev
å è½: 串å转
ç¨ æ³: char *strrev(char *str);
举ä¾ï¼
[cpp] view plain copy
#include <string.h>
#include <stdio.h>
int main(void)
{
char *forward = "string";
printf("Before strrev(): %s/n", forward);
strrev(forward);
printf("After strrev(): %s/n", forward);
return 0;
}
/*è¿è¡ç»æï¼
Before strrev(): string
After strrev(): gnirts
*/
å½æ°å: strstr
å è½: å¨ä¸²ä¸æ¥æ¾æå®å符串ç第ä¸æ¬¡åºç°
ç¨ æ³: char *strstr(char *str1, char *str2);
举ä¾ï¼
[cpp] view plain copy
#include <stdio.h>
#include <string.h>
int main(void)
{
char *str1 = "Borland International", *str2 = "nation", *ptr;
ptr = strstr(str1, str2);
printf("The substring is: %s/n", ptr);
return 0;
}
å½æ°å: strtod
å è½: å°å符串转æ¢ä¸ºdoubleåå¼
ç¨ æ³: double strtod(char *str, char **endptr);
举ä¾ï¼
[cpp] view plain copy
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char input[80], *endptr;
double value;
printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lf/n", input, value);
return 0;
}
å½æ°å: strtol
å è½: å°ä¸²è½¬æ¢ä¸ºé¿æ´æ°
ç¨ æ³: long strtol(char *str, char **endptr, int base);
举ä¾ï¼
[cpp] view plain copy
#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 = %ld/n", string, lnumber);
return 0;
}
å½æ°å: strupr
å è½: å°ä¸²ä¸çå°ååæ¯è½¬æ¢ä¸ºå¤§ååæ¯
ç¨ æ³: char *strupr(char *str);
举ä¾ï¼
[cpp] view plain copy
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
/* converts string to upper case characters */
ptr = strupr(string);
printf("%s/n", ptr);
return 0;
}
温馨提示:答案为网友推荐,仅供参考