现在的位置: 首页 > 综合 > 正文

字符串处理函数

2013年10月12日 ⁄ 综合 ⁄ 共 4853字 ⁄ 字号 评论关闭

(1)puts      向显示器输出字符串

        原型:int   puts(const char *s)     

        头文件:#include <stdio.h>

        返回值:成功返回输出的字符数,失败返回EOF

 

         puts()函数与printf()输出字符串的区别:

                  1.puts在输出字符串时,遇到'/0'会自动终止输出,并将'/0'转换为'/n'来输出

                  2.printf在输出字符串时,遇到'/0' 只是终止输出,并不会 '/0'转换为'/n'来输出

 

例子:

#include <stdio.h>

 

main()
{

 int   num;
 char a[10] = "abcd";
 char b[10] = "1234";
 num = puts(a);
 puts(b);

 printf("%d/n", num):
 printf("%s", a);
 printf("%s", b);
 printf("/n");
}

 

输出结果:

abcd     //(自动换行了,将‘/0’转换成‘  /n’ 了 )

1234    //(自动换行了,将‘/0’转换成‘  /n’ 了 )

5     //(返回值含有 ‘/0’,所以为num=5)

abcd1234

 

(2)strcat   字符串连接    

 strcat能够将字符串2连接到字符串1的后面。要注意的是字符串1必须能够装下字符串2。连接前,两串均以'/0'结束,连接后,串1的'/0'被取消,新串最后加‘'/0'

        原型:char  *strcat(char *s1, char *s2)     结果放在字符串1中

        头文件:#include <string.h>

        返回值:返回字符串数组1的首地址

 

        strlen   字符串长度

        头文件:#include <string.h>

        原型:int strlen(const char *s);

        返回值:返回字符串实际长度     不包含‘/0’ 在内

 

例子:

#include <stdio.h>
#include <string.h>

main()
{
 char a[10] = "abcd";
 char b[10] = "1234";
 strcat(a, b);
 printf("sizeof(a) =%d/nstrlen(a) = %d/n", sizeof(a), strlen(a));
 puts(a);
}

输出结果:

 sizeof(a) = 10

 strlen(a) = 8  //( strlen()为有效字符的长度,不含'/0')

 abcd1234

 

(3)  strcpy   字符串拷贝

          头文件:#include <string.h>

          原型:  char *strcpy(char *s1, char *s2)

          返回值:返回字符数组1的首地址

 

注:strcpy将字符串2,拷贝到字符数组1中去,要注意,字符数组1必须足够大,拷贝时'/0'一同拷贝,不能使用赋值语句为一个字符数组赋值。

 

 

例子:

#include <stdio.h>
#include <string.h>

main()
{
 char a[10] = "abcd";
 char b[10] = "123";
 strcpy(a, b);
 puts(a);
}

 

输出:

123   //将  ‘/0’也拷贝过去  遇到 ‘/0’结束  所以只输出123没有d

 

(4)strcmp    字符串比较

        头文件:#include <string.h>

        函数原型:int strcmp(char *s1, char *s2)

        返回值: 返回int型的整数

 

strcmp对两串从左向右逐个字符比较(ASCLL吗),直到遇到不同字符或'/0'为止。若s1大于s2返回正整数1,若s1小于s2返回负整数-1,若s1等于s2返回0。要注意字符串比较不能用"= =",必须用strcmp.

 

例子:

#include <stdio.h>
#include <string.h>

main()
{
 char a[10] = "abcd";
 char b[10] = "abcd";
 int result;
 result = strcmp(a, b);
 printf("%d/n", result);
}
输出:

 

(5)sprintf与fprintf区别:

 

 注意:这两个函数都不是输出

例子:

#include <stdio.h>

main()
{
 FILE *fp;
 int i;
 char a[10] = "abcd";
 char buffer[100];
 if((fp = fopen("wan", "w")) == NULL)
 {
  printf("can not open file!/n");
 }
 sprintf(buffer, "i love my mother!");
 printf("%s/n", buffer);
 fprintf(fp, "%s", a);
 for(i=0; i<3; i++)
 {
  fprintf(fp,"%s", buffer);
 }
 fclose(fp);
}

输出:

i love my mother!

文件wan中内容为:

abcdi love my mother!i love my mother!i love my mother!   //内容没有覆盖跟在后面

 

sprintf   格式化字符串复制

头文件:#include  <stdio.h>

原型:int sprintf(char *s1,  格式, char *s2)    //格式这个参数可没有,直接赋值

返回值:成功返回参数s1字符串的长度,失败返回-1

sprintf会把参数s2字符串转换为格式化数据,然后将结果复制到参数s1所指的字符数组,直到出现字符串结束符/0为止.

 

fprintf输出到文件中,sprintf输出到字符串中.
如:
fprintf(fp,"%s",name);
fp为文件指针    打到文件里面(FILE *)即:写文件

sprintf(buff,"%s",name);
buff为字符数组  打到字符数组里面(char *)即:字符数组赋值

 

 

(6) atoi与atof

例子:

#include <stdio.h>
#include <stdlib.h>

main()
{
 char a[10] = "1234";
// char b[10] = ""
 int b;
 float c;
 printf("b = %d/n", b = atoi(a));
 printf("c = %f/n", c = atof(a));
}

输出:

b = 1234

c = 1234.000000

 

 

 atoi   将字符串转换成整形数

头文件: #include <stdlib.h>

原型:int atoi(const char *s)

返回值:返回转换后的整形数

 

atoi会扫描参数s字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束符/0时结束转换,并将结束返回.

 

 atof   将字符串转换为浮点数

头文件:#include <stdlib.h>

原型:float atof(const char *s)

返回值:返回转换后的浮点数

 

atof会扫描参数s字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束符/0时结束转换,并将结果返回.

 

 

 (7) strtok    字符串分割

头文件:#include <string.h>

原型:char *strtok(char *s1, char *s2)

返回值:返回下一个分割后的字符串指针,如果无分割则返回NULL

 

Strtok可将字符串分割,当strtok在参数s1的字符串中发现到参数s2的分割字符时则会将该字符改为/0字符。在第一次调用时,strtok必须给予参数s字符串,往后的调用则将参数s设置为NULL.(这句话一定要记住

 

例子:

1:

#include <stdio.h>
#include <string.h>

main()
{
 char a[100] = "ada:-134-ij:cd:892kk-";
 char b[10] = ":-";
 char *p;
 printf("%s/n", strtok(a, b));
 while(p = strtok(a, b))
 {
  printf("%s/n", p);
 }
}
输出无数个:abc

 

2:

#include <stdio.h>
#include <string.h>

main()
{
 char a[100] = "ada:-134-ij:cd:892kk-";
 char b[10] = ":-";
 char *p;
 printf("%s/n", strtok(a, b));
 while(strtok(NULL, b))
 {
  printf("%s/n", strtok(a, b));
 }
}

只输出两个:
abc
abc

 

3:

#include <stdio.h>
#include <string.h>

main()
{
 char a[100] = "ada:-134-ij:cd:892kk-";
 char b[10] = ":-";
 char *p;
 printf("%s/n", strtok(a, b));
 while(strtok(NULL, b))
 {
  printf("%s/n", strtok(NULL, b));
 }
}

输出:
abc
ij
892kk

 

4:

#include <stdio.h>
#include <string.h>

main()
{
 char a[100] = "ada:-134-ij:cd:892kk-";
 char b[10] = ":-";
 char *p;
 printf("%s/n", strtok(a, b));
 while(p = strtok(NULL, b))
 {
  printf("%s/n", p);
 }
}

输出:
aba
134
ij
cd
892kk

 

8)  strcpy()  memset()  memcpy()比较:

#include <stdio.h>
#include <string.h>

main()
{
 char buffer[1024];
 char buffer1[1024] = "i love my mother guwan";
 strcpy(buffer, "i love my mother");
 printf("%s/n", buffer);
 fflush(stdin); 
 memcpy(buffer, buffer1, 30);            //将buffer1前30个字节copy到buffer中
 printf("%s/n", buffer);
 memset(buffer, '1', 50);                    //用于初始化,把buffer前50个字符都初始化为1
 printf("%s/n", buffer);
}
输出:

i love my mother

i love my mother guwan

111111111111111111111111111111111111111111111111

 

 

注意:memset()一般用于对字符串的赋值,最好不要用于整型。   memset()是以字节为单位赋值   整型4个字节

 

memcpy(),memset()有3个参数

 

mem是一段内存,他的长度,必须你自己记住
str也是一段内存,不过它的长度,你不用记,随时都可以计算出来
所以memcpy需要第三个参数,而strcpy不需要

 

 

 strncmp(char *s1, char *s2, int n)

功能:比较字符串s1和s2的前n个字符。
说明:
        当s1<s2时,返回值<0, 为一个负数s1减去s2的值
        当s1=s2时,返回值=0
        当s1>s2时,返回值>0     为一个正数s2减去s2的值

 

而strcmp(char *s1, char *s2)返回值:相等为0, 大于为1, 小于为-1

注意之间的区别

抱歉!评论已关闭.