#include<string.h>#include<stdio.h>intmain(void){char string[15];char*ptr, c ='r';strcpy(string,"This is a string");ptr =strchr(string, c);if(ptr)printf("The character %c is at position: %dn", c, ptr-string);elseprintf("The character was not foundn");return0;}
#include<string.h>#include<stdio.h>intmain(void){char*buf1 ="aaa",*buf2 ="bbb",*buf3 ="ccc";int ptr; ptr =strcmp(buf2, buf1);if(ptr >0)printf("buffer 2 is greater than buffer 1n");elseprintf("buffer 2 is less than buffer 1n"); ptr =strcmp(buf2, buf3);if(ptr >0)printf("buffer 2 is greater than buffer 3n");elseprintf("buffer 2 is less than buffer 3n");return0;}
#include<stdio.h>#include<string.h>#include<alloc.h>intmain(void){char*string1 ="1234567890";char*string2 ="747DC8";int length; length =strcspn(string1, string2);printf("Character where strings intersect is at position %dn", length);return0;}
#include<string.h>#include<stdio.h>intmain(void){char*buf1 ="BBB",*buf2 ="bbb";int ptr; ptr =strcmpi(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");return0;}
函數名: strncmp
功 能: 串比較 用 法: int strncmp(char *str1, char *str2, int maxlen); 程序例:
#include<string.h>#include<stdio.h>intmain(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 1n");elseprintf("buffer 2 is less than buffer 1n"); ptr =strncmp(buf2,buf3,3);if(ptr >0)printf("buffer 2 is greater than buffer 3n");elseprintf("buffer 2 is less than buffer 3n");return(0);}
#include<string.h>#include<stdio.h>intmain(void){char*buf1 ="BBBccc",*buf2 ="bbbccc";int ptr; ptr =strncmpi(buf2,buf1,3);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");return0;}
#include<stdio.h>#include<string.h>intmain(void){char*string ="abcdefghijklmnopqrstuvwxyz";char letter ='x';printf("string before strnset: %sn", string);strnset(string, letter,13);printf("string after strnset: %sn", string);return0;}
#include<stdio.h>#include<string.h>intmain(void){char*string1 ="abcdefghijklmnopqrstuvwxyz";char*string2 ="onm";char*ptr; ptr =strpbrk(string1, string2);if(ptr)printf("strpbrk found first character: %cn",*ptr);elseprintf("strpbrk didn't find character in setn");return0;}
#include<string.h>#include<stdio.h>intmain(void){char string[15];char*ptr, c ='r';strcpy(string,"This is a string"); ptr =strrchr(string, c);if(ptr)printf("The character %c is at position: %dn", c, ptr-string);elseprintf("The character was not foundn");return0;}
#include<stdio.h>#include<string.h>#include<alloc.h>intmain(void){char*string1 ="1234567890";char*string2 ="123DC8";int length; length =strspn(string1, string2);printf("Character where strings differ is at position %dn", length);return0;}
#include<stdio.h>#include<stdlib.h>intmain(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 %lfn", input, value);return0;}
#include<string.h>#include<stdio.h>intmain(void){char input[16]="abc,d";char*p;/* strtok places a NULL terminator in front of the token, if found */ p =strtok(input,",");if(p)printf("%sn", p);/* A second call to strtok using a NULL as the first parameter returns a pointer to the character following the token */ p =strtok(NULL,",");if(p)printf("%sn", p);return0;}
函數名: strtol
功 能: 將串轉換為長整數 用 法: long strtol(char *str, char **endptr, int base); 程序例:
#include<stdlib.h>#include<stdio.h>intmain(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);return0;}
#include<stdlib.h>#include<stdio.h>#include<string.h>char source[15]="rFna koBlrna d";char target[15];intmain(void){swab(source, target,strlen(source));printf("This is target: %sn", target);return0;}
isalpha()
PS:isalpha()是字符函數,不是字符串函數 原型:extern int isalpha(int c); 用法:#include <ctype.h> 功能:判斷字符c是否為英文字母 說明:當c為英文字母a-z或A-Z時,返回非零值,否則返回零。 舉例:
#include<syslib.h>#include<ctype.h>#include<stdio.h>main(){int c;clrscr();// clear screenprintf("Press a key");for(;;){c=getchar();clrscr();printf("%c: %s letter",c,isalpha(c)?"is":"not");}return0;// just to avoid warnings by compiler}