注:编码工具是CLion+Cygwin64
目录
字符串的两种定义方式
自定义获取字符串长度方法
方式一:
方式二:
字符串转数字
比较
查找和包含
拷贝和拼接
大小写转换
自定义字符串截取函数、
一:
二:
三:
四:
五:
方式一:字符数组
此种方式需要在最后一个元素后面添加'\0',C语言的字符串以'\0'结尾。
方式二:字符指针
此种方式隐式添加'\0'。
#include
int main(){
char str[7] = {'S', 't', 'r', 'i', 'n', 'g'};
printf("str = %s\n", str);
char str3[] = {'C', 'o', 'l', 'd', '\0'};
printf("str3 = %s\n", str3);
str3[0] = 'F';
printf("str3 = %s\n", str3);
char * str2 = "Here I Am";
printf("str2 = %s\n", str2);
// str2[2] = 'e'; // 不能如此操作
// printf("str2 = %s\n", str2);
return 0;
}
输出:
str = String
str3 = Cold
str3 = Fold
str2 = Here I Am
#include
int len(char *str) {
char *tmp = str;
int count = 0;
while (*tmp) {
count++;
tmp++;
}
return count;
}
int main() {
char *str = "Better Babe";
int length = len(str);
printf("str's length is %d\n", length);
return 0;
}
输出:
str's length is 11
#include
void len(int * length, char *str) {
char *tmp = str;
int count = 0;
while (*tmp) {
count++;
tmp++;
}
*length = count;
}
int main() {
char str[] = {'H', 'e', 'r', 'e', ' ', 'I', ' ', 'A', 'm', '\0'};
int length;
len(&length, str);
printf("str's length is %d\n", length);
return 0;
}
输出:
str's length is 9
注意:C/C++编译器中,数组作为参数传递,会把数组优化为指针。
#include
#include
int main() {
char * str = "99";
char * str2 = "4.596";
char * str3 = "16.88x";
char * str4 = "xyz186";
int i = atoi(str);
int i2 = atoi(str2);
int i3 = atoi(str3);
int i4 = atoi(str4);
printf("i = %d, i2 = %d, i3 = %d, i4 = %d\n", i, i2, i3, i4);
long l = atol(str);
long l2 = atol(str2);
long l3 = atol(str3);
long l4 = atol(str4);
printf("l = %ld, l2 = %ld, l3 = %ld, l4 = %ld\n", l, l2, l3, l4);
double d = atof(str);
double d2= atof(str2);
double d3= atof(str3);
double d4 = atof(str4);
printf("d = %lf, d2 = %lf, d3 = %lf, d4 = %lf\n", d, d2, d3, d4);
return 0;
}
输出:
i = 99, i2 = 4, i3 = 16, i4 = 0
l = 99, l2 = 4, l3 = 16, l4 = 0
d = 99.000000, d2 = 4.596000, d3 = 16.880000, d4 = 0.000000
#include
#include
#include
#include
#include
int main() {
char * str = "One Day";
char * str2 = "One Day";
char * str3 = "one day";
int r = strcmp(str, str2); // 区分大小写
if(r){
printf("str(%s)和str2(%s)不相等\n", str, str2);
}else{
printf("str(%s)和str2(%s)相等\n", str, str2);
}
int r2 = strcmp(str, str3); // 区分大小写
if(r2){
printf("str(%s)和str3(%s)不相等\n", str, str3);
}else{
printf("str(%s)和str3(%s)相等\n", str, str3);
}
int r3 = strcasecmp(str, str3); // 不区分大小写
if(r3){
printf("str(%s)和str3(%s)忽略大小写后不相等\n", str, str3);
}else{
printf("str(%s)和str3(%s)忽略大小写后相等\n", str, str3);
}
return 0;
}
输出:
str(One Day)和str2(One Day)相等
str(One Day)和str3(one day)不相等
str(One Day)和str3(one day)忽略大小写后相等
查找到了就是包含,否则就是不包含。
#include
#include
int main() {
char * str = "Do You Love Me?";
char * substr = "L";
char * result = strstr(str, substr);
if(result)
{
printf("在'%s'中找到了'%s'\n", str, substr);
printf("result = %s\n", result);
printf("'%s'在'%s'中的位置是:%d\n", substr, str, result - str);
printf("'%s'\"包含\"'%s'\n", str, substr);
}else{
printf("在'%s'没找到了'%s'\n", str, substr);
printf("'%s'\"不包含\"'%s'\n", str, substr);
}
return 0;
}
输出:
在'Do You Love Me?'中找到了'L'
result = Love Me?
'L'在'Do You Love Me?'中的位置是:7
'Do You Love Me?'"包含"'L'
#include
#include
int main(){
char * result = malloc(100);
char * to = "--到--", *cpp = "C++", *java = "Java";
if(result){
strcpy(result, java);
printf("result = %s\n", result);
strcat(result, to);
printf("result = %s\n", result);
strcat(result, cpp);
printf("result = %s\n", result);
free(result);
result = NULL;
}
return 0;
}
输出:
result = Java
result = Java--到--
result = Java--到--C++
利用ctype头文件中的tolower和toupper方法编写字符串的大小写转换方法。
#include
#include
void lower(char *result, char *str) {
char *tmp = str;
while (*tmp) {
*result = tolower(*tmp);
result++;
tmp++;
}
}
void upper(char *result, char *str) {
char *tmp = str;
while (*tmp) {
*result = toupper(*tmp);
result++;
tmp++;
}
}
int main() {
char *str = "Yesterday Once More";
char *low, *up;
lower(low, str);
upper(up, str);
printf("low = %s\nup = %s\n", low, up);
return 0;
}
输出:
low = yesterday once more
up = YESTERDAY ONCE MORE
如果result被赋了值,如:char * result = "ABC"或者char * result = NULL,因为字符串被保存在全局区,而NULL也有自己的内存区域,这两个区域都不能修改,所以以下函数在这两种情况下无效。
void substring(char *result, char *src, int start, int end) {
char *tmp = src;
int count = 0;
while (*tmp) {
if (count >= start && count < end) {
*result = *tmp;
result++;
}
tmp++;
count++;
}
}
int main() {
char * src = "GenieForEverybody";
char * result;
substring(result, src, 0, 5);
printf("result = %s\n", result);
return 0;
}
输出:
result = Genie
void substring(char **result, char *src, int start, int end) {
char *tmp = src;
// 动态开辟
char * r = malloc(end - start);
int i;
for(i = start; i < end; i ++){
r[i - start] = tmp[i];
}
*result = r;
}
int main() {
char * src = "GenieForEverybody";
char * result;
substring(&result, src, 0, 5);
printf("result = %s\n", result);
if(result)
{
free(result);
result = NULL;
}
return 0;
}
输出:
result = Genie
void substring(char *result, char *src, int start, int end) {
char *tmp = src;
// 静态开辟
char r[end - start];
int i;
for(i = start; i < end; i ++){
r[i - start] = tmp[i];
}
strcpy(result, r);
}
int main() {
char * src = "Amaninakupenda";
char * result;
substring(result, src, 0, 5);
printf("result = %s\n", result);
return 0;
}
输出:
result = Amani
void substring(char *result, char *src, int start, int end) {
int i;
for(i = start; i < end; i ++){
*(result + i - start) = *(src +i);
}
}
int main() {
char * src = "Amaninakupenda";
char * result;
substring(result, src, 0, 5);
printf("result = %s\n", result);
return 0;
}
输出:
result = Amani
void substring(char *result, char *src, int start, int end) {
strncpy(result, src + start, end - start);
}
int main() {
char * src = "Amaninakupenda";
char * result;
substring(result, src, 0, 5);
printf("result = %s\n", result);
return 0;
}
输出:
result = Amani