c_cpp STRCMP()

int strcmp(const char * str1,const char * str2); <br/> <br/>比较两个字符串<br/>将C字符串str1与C字符串str2进行比较。 <br/> <br/>此函数开始比较每个字符串的第一个字符。如果它们彼此相等,则继续使用以下对,直到字符不同或直到达到终止空字符。 <br/> <br/>此函数执行字符的二进制比较。有关考虑特定于语言环境的规则的函数,请参阅strcoll。

strcmp.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	cout << strcmp("aa", "ab") << endl;	// -1
	cout << strcmp("aa", "aa") << endl;	// 0
	cout << strcmp("ab", "aa") << endl;	// 1
}

c_cpp 函数strncpy()

char * strncpy(char * destination,const char * source,size_t num); <br/> <br/>从字符串<br/>复制字符将源的第一个num字符复制到destination。如果在复制num个字符之前找到源C字符串的结尾(由空字符表示),则使用零填充目标,直到已向其写入总共num个字符。 <br/> <br/>如果source长于num,则不会在目标末尾隐式附加空字符。因此,在这种情况下,目标不应被视为空终止的C字符串(因此读取它会溢出)。 <br/> <br/>目的地和来源不得重叠(重叠时请参阅memmove以获得更安全的替代方案)。 <br/>

strncpy.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	char str1[] = "Hello world!";
	char str2[30] = "";
	cout << strncpy(str2, str1, 7) << endl;	// Hello w
}

c_cpp 的strcpy()

char * strcpy(char * destination,const char * source); <br/>复制字符串<br/>将source指向的C字符串复制到destination指向的数组中,包括终止空字符(并在该点停止)。 <br/>为避免溢出,目标指向的数组大小应足够长,以包含与源相同的C字符串(包括终止空字符),并且不应在内存中与源重叠。

strcpy.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	char str1[] = "Hello world!";
	char str2[30] = "";
	cout << strcpy(str2, str1) << endl;	// Hello world!
}

c_cpp strcat的()

char * strcat(char * destination,const char * source); <br/>连接字符串<br/>将源字符串的副本追加到目标字符串。目标中的终止空字符被源的第一个字符覆盖,并且在由目标中的两个串联形成的新字符串的末尾包括空字符。 <br/>目的地和来源不得重叠。

strcat.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	char str1[30] = "Hello ";
	char str2[] = "world!";
	cout << strcat(str1, str2) << endl;	// Hello world!
}

c_cpp 放()

int puts(const char * str);

test_put.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	// string str1 = "Hello\nworld";	// cannot convert argument 1 from 'std::string' to 'const char *'
	char str1[] = "Hello\nworld";
	puts(str1);
	/*
	 * Hello
	 * world
	 */
	return 0;
}

c_cpp int➡string

int2string.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	string s = to_string(123) + "45";
	cout << s << endl;	// 12345
	return 0;
}

c_cpp 计数()

统计字符串中某个字符出现的次数

count.cpp
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
	string str1 = "abbcccdddddeeeffg0$";
	cout << count(str1.begin(), str1.end(), 'e') << endl;	// 3
	return 0;
}

c_cpp decimal2binary

decimal2binary.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	int d = 11;
	cout << d << endl;	// 11
	string b = "";
	while (d) {
		b = to_string(d % 2) + b;
		d /= 2;
	}
	cout << b << endl;	// 1011
	return 0;
}
itoa.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	int value = 11;
	cout << value << endl;	// 11
	char str1[10];
	_itoa_s(value, str1, 2);
	cout << str1 << endl;	// 1011
	return 0;
}

c_cpp int↔char

int_char.cpp
#include <iostream>
using namespace std;

int main() {
	// ASCII (S: 83)
	int a = 339;
	char b = a;
	char c = (char)a;
	int d = (int)b;
	int e = a & 0xff; // oxff = 256
	cout << a << " " << b << " " << c << " " << d << " " << e << endl; // 339 S S 83 83
}

c_cpp char *➡字符串

char2string.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	char str1[20] = "Hello world!";
	string str2(str1);
	cout << str1 << "  " << str2 << endl; // Hello world!  Hello world!
	return 0;
}