C语言字符串相关函数

2018-03-30  本文已影响21人  你好星期四

gets

原型:char *gets(char *string)

gets函数会保留空格,当输入回车时才认为输入完成。gets可能会有缓冲区溢出的危险。

#include <stdio.h>

int main(){
  char s[100] = {0};
  gets(s);
  printf("%s\n", s);
  return 0;
}

fgets

原型:char *fgets(char *buf, int bufsize, FILE *stream)

fgets会认为回车也是输入的一部分。fgets指定了缓冲区大小,因此是安全的。

#include <stdio.h>

int main(){
  char s[100] = {0};
  fgets(s, sizeof(s), stdin);
  printf("%s", s);
  return 0;
}

puts

原型:int puts(const char *string)

puts会自动在输出中加上一个 \n 。

#include <stdio.h>

int main(){
  char s[] = "hello world!";
  puts(s);
  return 0;
}

fputs

原型:int fputs(const char *str, FILE *stream)

fputs不会自动加 \n 。

#include <stdio.h>

int main(){
  char s[] = "hello world!\n";
  fputs(s, stdout);
  return 0;
}

strlen

原型:extern unsigned int strlen(char *s)

strlen用于获取字符串有效长度,不包括\0。此函数需要包含头文件string.h

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

int main(){
  char s[100] = "hello world!";
  int len = strlen(s);
  printf("%d\n", len); // 12
  return 0;
}

strcat

原型:extern char *strcat(char *dest, const char *src)

把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0'),src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。

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

int main(){
  char a[100] = "hello";
  char b[100] = " world";
  strcat(a, b);
  printf("%s\n", a); // hello world
  return 0;
}

strncat

原型:char * strncat(char *dest, const char *src, size_t n)

把src所指字符串的前n个字符添加到dest所指字符串的结尾处,并覆盖dest所指字符串结尾的'\0'。

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

int main(){
  char a[100] = "hello";
  char b[100] = "world";
  strncat(a, b, 1);
  printf("%s\n", a); // hellow
  return 0;
}

strcmp

原型:extern int strcmp(const char *s1,const char *s2)

用于比较两个字符串大小。

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

int main(){
  char a[] = "hello";
  char b[] = "world";
  int cmp = strcmp(a, b);
  if(cmp > 0){
    printf("a大");
  }else if(cmp <0){
    printf("b大");
  }else{
    printf("一样大");
  }
}

strncmp 用于比较前n个字符的大小。原型为 int strncmp ( const char * str1, const char * str2, size_t n )

strcpy

原型:char *strcpy(char* dest, const char *src)

把从src地址开始且含有\0结束符的字符串复制到以dest开始的地址空间。src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。

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

int main(){
  char a[100] = "hello";
  char b[100] = "world";
  strcpy(a, b);
  printf("%s\n", a); // world
  return 0;
}

strncpy

同strcpy,只是可以指定只拷贝前n个字符串。

原型:char *strncpy(char *dest,char *src,size_t n)

如果src的前n个字符不含\0字符,则结果不会以\0字符结束,需要再手动添加一个'\0'。

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

int main(){
  char a[100] = "";
  char b[100] = "world";
  strncpy(a, b, 1);
  a[2] = '\0';
  printf("%s\n", a); // w
}

sprintf

原型:int sprintf( char *buffer, const char *format, [ argument] … )

把格式化的数据写入某个字符串缓冲区。

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

int main(){
  char buffer[100];
  sprintf(buffer, "%d", 100);
  printf("%s\n", buffer);
  return 0;
}

sscanf

int sscanf(const char *buffer, const char *format, [argument] ...)

sscanf读取格式化的字符串中的数据。

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

int main(){
  char buffer[100];
  int a;
  sscanf("100 abc", "%d %s", &a, buffer);
  printf("%d %s\n", a, buffer); // 100 abc
  return 0;
}

strchr

原型:extern char *strchr(const char *s,char c)

可以查找字符串s中首次出现字符c的位置。

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

int main(){
  char s[100] = "hello world";
  char *a = strchr(s, 'o');
  printf("%s\n", a); // o world
  return 0;
}

strstr

原型:extern char *strstr(char *str1, const char *str2)

在字符串中查找子串

strtok

原型:char *strtok(char s[], const char *delim)

分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串

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

int main(){
  char s[100] = "hello world nihao shijie";
  char *a = strtok(s, " ");
  printf("%s\n", a); // hello
  a = strtok(NULL, " ");
  printf("%s\n", a); // world
  a = strtok(NULL, " ");
  printf("%s\n", a); // nihao
  a = strtok(NULL, " ");
  printf("%s\n", a); // shijie
  return 0;
}

atoi

原型:int atoi(const char *nptr)

将一个字符串转为整数。需要包含头文件stdlib.h。

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

int main(){
  char a[100] = "123";
  int i = atoi(a);
  printf("%d\n", i); // 123
  return 0;
}

类似的还有atof转为浮点数, atol转为long。

上一篇 下一篇

猜你喜欢

热点阅读