C/C++ 常用字符串函数总结

#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

/* cstdio/stdio.h */
// stream/character input usually return EOF when end-of-file encountered
int fprintf(FILE *fp, const char *fmt, ...); // to fp
int fscanf(FILE *fp, const char *fmt, ...); // from fp
int printf(const char *fmt, ...); // to stdout
int scanf(const char *fmt, ...); // from stdin
int sprintf(char *s, const char *fmt, ...); // to stirng
int sscanf(char *s, const char *fmt, ...); // from string

int getchar(void); // from stdin
int putchar(int ch); // to stdout
int getc(FILE *fp); // may be macro
int fgetc(FILE *fp); // equivalent to getc
int putc(int ch, FILE *fp); // may be macro
int fputc(int ch, FILE *fp); // equivalent to putc

char * fgets(char *str, int num, FILE *fp);
/*
	This function return read from stream specified by fp.
	It reads a maximum of num characters, including '\0',
	which is appended automatically.
	if '\n' is encountered before it can read num-1 characters,
	'\n' is also copied to str.

	Return NULL if nothing can be read.
*/
char * gets(char *str);
/*
	This function read str from stdin.
	Difference between fgetc:
	1. '\n' not copied, but '\0' is appended.
	2. no buffer overflow checking.
*/

/* cctype/ctype.h */
// Note that EOF can be passed as argument
int isalnum(int c);
int isalpha(int c);
int isdigit(int c);
int islower(int c);
int isupper(int c);
int isspace(int c); // ' ', '\n', '\t', '\v', '\f', '\r'

int tolower(int c);
int toupper(int c);

/* cstring/string.h */
char * strcpy(char *dst, const char *src);
char * strcat(char *dst, const char *src); // append at '\0' of dst
int strcmp(const char *str1, const char *str2);
/*
	> 0: str1 > str2
	==0: str1 == str2
	< 0: str1 < str2
*/
char * strstr(char *str1, const char *str2);
/*
	returns a ptr to the first occurence of str2 in str1,
	or NULL if not found
*/
size_t strlen(const char *str);

/* string */
istream& getline(istream& is, string& str);
/*
	Read a line from istream, '\n' is extracted and discarded
	The original content of str is replaced. (The same as operator>>)
*/
istream& operator>>(istream& is, string& str); // read a word from istream
size_t size() const; // O(1) in C++11
void clear() const; // usually O(1)
char& operator[](size_t n);
string& operator+=(const string& str);
string& operator+=(const char *str);
string& operator+=(char ch);
const char* c_str() const; // return C string equivalent in O(1) time
size_t find(const string& str, size_t pos = 0);
/*
	Search for the first occurence of str starting from pos (inclusive).
	Return the position (index) of first occurence if found,
	or string::npos if not found.
*/
string substr(size_t pos = 0, size_t len = npos);
/*
	Return substring from pos with length len.
	If end of string is encountered first, only return this part.
	Default value npos of len means to the end of string.
*/

// convert to/from string
int stoi(const string& str, size_t *idx = 0, int base = 10);
/*
	Return converted integer with specified base.
	If idx != NULL, it is set to the next position after
	the converted number.
	Similar functions for conversion to long / long long /
	float / double also exist.
	May throw invalid_argument exception.
*/
string to_string(); // convert integral/floating point to string


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值