字符处理函数集(1)

来源:互联网 发布:老虎机奖池js效果 编辑:程序博客网 时间:2024/05/01 15:34

/*******************

1.atoi

int atoi(const char *)

*********************/

#include <stdlib.h>

#include <stdio.h>

int main()

{

  char *str="101";

  long num;

  num=atoi(str);

  printf("str=%s num%d/n,str,num");

  return 0;

}

/********************************

2.memset,memcpy

将缓冲区设置为某个字符/将缓冲区中的内容拷贝n个到目的地址

void  *memset(void *int size_t)

void *memcpy(void *,const void *,size_t)

*********************************/

#include <stdio.h>

#include <string.h>

void main()

{

  char buf[]="hello world";

  char str[20];

  memcpy(str,buf,5);

  printf("调用memset之前,str=%s/n",str);

  memset(str,'/0'.sizeof(str));

  memcpy(string,buf,5);

printf("调用memset之后,str=%s/n",str);

}

 

待续

/*函数setbuf

 功能 :把缓冲区和流相联

用法: void setbuf(FILE *steam,char *buf)

*/

#include <stdio.h>

int main()

{

   char outbuf[20];

  memset(outbuf,0,sizeof(outbuf));

  setbuf(stdout,outbuf);

   printf("hello");

  fflush(stdout);

  return 0;

}

 /*函数名:

  功能:送格式化输出到字符串

  用法:int sprintf(char *str,char *faomat [,argument,.....]);

*/

#include <stdio.h>

#include <math.h>

int main()

{

  char buffer[];

  int count=120;

  sprintf(buffer,"%d",count);

  puts(buffer);

  return 0;

}

/*

函数名:strcat

功能:字符串拼接函数

用法:char *strcat(char *destin,char *source)

*/

#include <string.h>

#include <stdio.h>

int main()

{

  char destination[25];

  char *blank=" ";

  strcpy(destination,Borland);

  strcat(destination,blank);

   strcat(destination,c);

  printf("%s/n",destination);

   return 0;

}

/*

    函数名:strchr

   功能:在一个字符串中查找给定字符的第一个匹配之处

   用法:char *strchr(char *str,char c)

*/

 #include <string.h>

 #include<stdio.h>

  int main()

{

   char string[15];

   char *ptr,c='r';

   strcpy(string,"this is astring");

   ptr=strchr(string,c);

   if(ptr)

   {

    printf("the charactor %c is at position :%d/n",c,ptr-string);

   } 

else

  {

    printf("the charactor was not found/n");
 

  }

   return 0;

}

 /* 
函数名: strcmp
功  能: 串比较
用  法: int strcmp(char *str1, char *str2);
 
*/

 #include <string.h>

 #include <stdio.h>

  int main()

  {

   char *buf1="aaa";

    char *buf2="bbb";

    char *buf3="ccc";

    int ptr;

    ptr=strcmp(buf2,buf1);

   if(ptr>0)

  {

   printf("buf2 is greater than buffer 1/n");

  }

  else

  {

   printf("buffer 2 is less than buffer 1/n");

   }

  ptr=strcmp(buf2,buf3)

  {

   if(ptr>0)

  {

  printf("buffer 2 is greater than buffer 3/n");

  }

else

{

  printf("buffer 2 id less than buffer 3/n");

 }

 }

 /*

  函数名: strtod
功  能: 将字符串转换为double型值
用  法: double strtod(char *str, char **endptr);

*/

#include <stdio.h>

#incldue <stdlib.h>

int main()

{

 char input[80],*endptr;

 double valude;

 printf("enter a floating point number:");

gets(input);

value=strtod(input,&endptr);

printf("the string is %s the number is %f",input,value);

return 0;
}

/*

函数名:strtok

功能:查找在第二个串中指定的分界符隔开的单词

用法:char *strtok(char *str1,char *str2)

*/

#include <string.h>

#include <stdio.h>

int main()

{

char input[16]="abc,d";

char *p;

p=strtok(input,",");

if(p)

{

printf("%s/n",p);

}
}

原创粉丝点击