目录
字符串函数与内存函数::
字符串函数:
字符串长度:
strlen:
注意:
1.字符串以 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0')
2.参数指向的字符串必须以 '\0' 结束.
3.注意函数的返回值是size_t,是无符号的.
//下列代码的运行结果是:
int main()
{
if (strlen("abc") - strlen("abcdef") > 0)
{
printf(">\n");
}
else
{
printf("<=\n");
}
return 0;
}
//程序的输出结果是:>
模拟实现strlen:
size_t my_strlen(const char* str)
{
assert(str);
size_t count = 0;
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
char arr[] = "abcdef";
size_t n = my_strlen(arr);
printf("%u\n", n);
return 0;
}
长度不受限制的字符串函数:
strcpy:
练习:
#include<stdio.h>
#include<string.h>
int main()
{
char ch[] = "hello world";
char str[100]={ 0 };
strcpy(str, ch);
printf("%s\n", str);
return 0;
}
注意:
1.源字符串必须以 '\0' 结束.
2.会将源字符串中的 '\0' 拷贝到目标空间.
3.目标空间必须可变.
模拟实现strcpy:
#include<stdio.h>
char* my_strcpy(char* dest, char* src)
{
assert(dest && src);
char* ret = dest;
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char name[20] = { 0 };
my_strcpy(name, "zhangsan");
printf("%s\n", name);
return 0;
}
strcat:
练习:
#incluude<string.h>
#include<stdio.h>
int main()
{
char dest[100] = "hello";
char src[] = "world";
strcat(dest, src);
printf("%s\n", dest);
return 0;
}
注意:
1.源字符串必须以 ‘\0’ 结束.
2.目标空间必须有足够大的空间,能容纳下源字符串的内容.
3.目标空间必须可修改.
4.不能用于自己给自己追加.
模拟实现strcat:
char* my_strcat(char* dest, char* src)
{
assert(dest && src);
char* ret = dest;
while (*dest)
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = "hello";
my_strcat(arr1, "world");
printf("%s\n", arr1);
return 0;
}
//strcat不能自己给自己追加 会导致原字符串\0丢失 程序陷入死循环
strcmp:
练习:
int main()
{
char arr1[] = "zhangsan";
char arr2[] = "zhangsanfeng";
if (arr1 == arr2)
{
printf("==\n");
}
else
{
printf("!=\n");
}
return 0;
}
int main()
{
char arr1[20] = "zhangsan";
char arr2[20] = "zhangsanfeng";
//两个字符串比较相等应该使用strcmp
int ret = strcmp(arr1, arr2);
if (ret < 0)
printf("<\n");
else if (ret == 0)
printf("==\n");
else
printf(">\n");
return 0;
}
标准规定:
1.第一个字符串大于第二个字符串,则返回大于0的数字.
2.第一个字符串等于第二个字符串,则返回0.
3.第一个字符串小于第二个字符串,则返回小于0的数字.
模拟实现strcmp:
#include<assert.h>
#include<stdio.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
return *str1 - *str2;
}
int main()
{
char arr1[20] = "zhangsan";
char arr2[20] = "zhangsanfeng";
int ret = my_strcmp(arr1, arr2);
if (ret < 0)
printf("<\n");
else if (ret == 0)
printf("==\n");
else
printf(">\n");
return 0;
}
长度受限制的字符串函数:
strncpy
练习:
int main()
{
char arr1[] = "hello";
char arr2[] = "world";
strncpy(arr1, arr2, 5);
printf("%s\n", arr1);
return 0;
}
注意:
1.拷贝n个字符从源字符串到目标空间.
2.如果源字符串的长度小于n,则拷贝完源字符串之后,在目标的后边追加0,直到n个.
模拟实现strncpy:
char* my_strncpy01(char* dest, const char* src, size_t n)
{
assert(dest && src);
char* ret = dest;
int i = 0;
while (i < n && *(src + i) != '\0')
{
*(dest + i) = *(src + i);
i++;
}
return ret;
}
char* my_strncpy02(char* dest, const char* src, size_t n)
{
assert(dest && src);
char* ret = dest;
while (n-- && *src)
{
*dest = *src;
dest++;
src++;
}
return ret;
}
char* my_strncpy03(char* dest, const char* src, size_t n)
{
assert(dest && src);
char* ret = dest;
while ((*dest++ = *src++) && --n)
{
;
}
return ret;
}
strncat
练习:
int main()
{
char arr1[20] = "hello ";
char arr2[] = "world";
strncat(arr1, arr2, 5);
printf("%s\n", arr1);
return 0;
}
模拟实现strncat:
//字符串的有限追加再把约束的有限内容追加过去后也会将\0追加过去
#include<stdio.h>
#include<string.h>
char* my_strncat(char* dest, const char* src, size_t n)
{
assert(dest && src);
char* ret = dest;
while (*dest)
{
dest++;
}
while ((*dest++ = *src++) && --n)
{
;
}
return ret;
}
int main()
{
char dest[100] = "hello ";
char src[] = "world";
my_strncat(dest, src, 5);
printf("%s\n", dest);
return 0;
}
strncmp
练习:
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abc";
int ret = strncmp(arr1, arr2, 3);
if (ret == 0)
printf("==\n");
else if (ret < 0)
printf("<\n");
else
printf(">\n");
return 0;
}
注意:比较到出现两个字符不一样或者一个字符串结束,或者n个字符全部比较完.
模拟实现strncmp:
int my_strncmp(const char* str1, const char* str2, size_t n)
{
assert(str1 && str2);
for (int i = 0; i < n; i++)
{
if (*str1 != *str2)
{
return *str1 - *str2;
}
str1++;
str2++;
}
return 0;
}
int main()
{
char arr1[] = "hello world";
char arr2[] = "hallo world";
int ret = my_strncmp(arr1, arr2, 5);
if (ret == 0)
printf("==\n");
else if (ret < 0)
printf("<\n");
else
printf(">\n");
return 0;
}
字符串查找函数:
strstr
strstr中的while模型和do...while模型
用途:利用strstr标准库函数找出一个字符串中substr(子串)出现的个数.
(a)while模型
int main()
{
char* str = "11abcd111122abcd333abcd3322abcd33333322qqq";
char ch[] = "abcd";
char* p = strstr(str, ch);
int count = 0;
while (p != NULL)
{
count++;
p += strlen(ch);
p = strstr(p, ch);
}
printf("abcd在字符串中出现次数:%d次\n", count);
return 0;
}
(b)do...while模型
int main()
{
char* str = "11abcd111122abcd333abcd3322abcd33333322qqq";
char ch[] = "abcd";
char* p = strstr(str, ch);
int count = 0;//记录个数
do
{
if (p != NULL)
{
count++;
p += strlen(ch);
p = strstr(p, ch);
}
} while (p);
printf("abcd在字符串中出现次数:%d次\n", count);
return 0;
}
模拟实现strstr:
//返回值为第一次字符串出现的起始位置 或者NULL
//在原字符串找到和目标字符串的起始位置时 需要在原字符串中记录
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
const char* s1 = str1;
const char* s2 = str2;
const char* p = str1;
while (*s1)
{
p = s1;
while (*s1 && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return (char*)p;
}
s1 = p + 1;
s2 = str2;
}
return NULL;
}
int main()
{
char email[] = "wjq@tengxunkeji.com";
char substr[] = "tengxunkeji";
char* ret = my_strstr(email, substr);
if (ret == NULL)
printf("子串不存在\n");
else
printf("%s\n", ret);
return 0;
}
strtok
练习:
#include<string.h>
#include<stdio.h>
int main()
{
const char* sep = "@.";
char email[] = "tengxunkeji@123456.com";
char cp[30] = { 0 };
strcpy(cp, email);
char* ret = strtok(cp, sep);
printf("%s\n", ret);
ret = strtok(NULL, sep);
printf("%s\n", ret);
ret = strtok(NULL, sep);
printf("%s\n", ret);
return 0;
}
//简化写法
int main()
{
const char* sep = "@.";
char email[] = "tengxunkeji@123456.com.net";
char cp[30] = { 0 };
strcpy(cp, email);
char* ret = NULL;
for (ret = strtok(cp, sep);ret != NULL;ret = strtok(NULL,sep))
{
printf("%s\n", ret);
}
return 0;
}
错误信息报告函数:strerror
功能:返回错误码对应的错误信息.
C语言的库函数在执行失败的时候都会设置错误码
#include<errno.h>
#include<string.h>
#include<stdio.h>
int main()
{
printf("%s\n", strerror(0));//NO error
printf("%s\n", strerror(1));//Operation not permitted
printf("%s\n", strerror(2));//No such file or directory
printf("%s\n", strerror(3));//No such process
//errno是C语言设置的一个全局的错误码存放的变量
return 0;
}
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
return 0;
}
错误信息报告函数:perror
功能:打印错误信息,相当于printf("%s\n", strerror(errno));
#include<stdio.h>
struct S
{
char arr[10];
int age;
float score;
};
int main()
{
struct S s = { "zhangsan",25,50.0f };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fprintf(pf, "%s,%d,%f", s.arr, s.age, s.score);
fclose(pf);
pf = NULL;
return 0;
}
字符分类函数
函数 | 如果它的参数符合下列条件就返回真 |
iscntrl | 任何控制字符 |
isspace |
空白字符:空格
‘ ’
,换页
‘\f’
,换行
'\n'
,回车
‘\r’
,制表符
'\t'
或者垂直制表符
'\v'
|
isdigit
|
十进制数字
0~9
|
isxdigit
|
十六进制数字,包括所有十进制数字,小写字母
a~f,大写字母A~F
|
islower
|
小写字母
a~z
|
isupper
|
大写字母
A~Z
|
isalpha
|
字母
a~z
或
A~Z
|
isalnum
|
字母或者数字,
a~z,A~Z,0~9
|
ispunct
|
标点符号,任何不属于数字或者字母的图形字符(可打印)
|
isgraph
|
任何图形字符
|
isprint
|
任何可打印字符,包括图形字符和空白字符
|
字符转换函数:
int tolower(int c)
int toupper(int c)
字符串类型转换函数:atoi
类似的函数还有:
atof():把一个小数形式的字符串转化为一个浮点数.
atol():把一个字符串转化为 long 类型.
//atoi会自动忽略空格 从正负号开始 直到遇到字母结束
//atoi会自动扫描字符串 跳过前面的空格 直到遇到数字或正负号才开始做转换而遇到非数字才结束转换
#include<stdio.h>
int main()
{
char ch[] = "123456";
int i = atoi(ch);
printf("%d\n", i);
return 0;
}
《剑指offer》的一道经典笔试题:模拟实现atoi
#include<stdlib.h>
#include<assert.h>
#include<stdio.h>
#include<ctype.h>
#include<limits.h>
enum Status
{
VALID,
INVALID
}sta = INVALID;//默认非法
int my_atoi(const char* str)
{
assert(str);
int flag = 1;
if (*str == '\0')
return 0;//非法0
//跳过空白字符
while (isspace(*str))
{
str++;
}
if (*str == '+')
{
flag = 1;
str++;
}
else if (*str == '-')
{
flag = -1;
str++;
}
long long ret = 0;
while (*str)
{
if (isdigit(*str))
{
ret = ret * 10 + flag*(*str - '0');
if (ret > INT_MAX || ret < INT_MIN)
{
return 0;
}
}
else
{
return ret;
}
str++;
}
if (*str == '\0')
{
sta = VALID;
}
return (int)ret;
}
扩:字符串其他常见模型
1.字符串中统计每个字符出现个数:
int main()
{
//统计字符出现个数
char ch[] = "numberofoccurrencesofstring";
//存储字符出现次数
int arr[26] = { 0 };
int i = 0;
for (i = 0; i < strlen(ch); i++)
{
arr[ch[i] - 'a']++;
}
for (i = 0; i < 26; i++)
{
printf("字母:%c出现次数:%d\n", i + 'a', arr[i]);
}
return 0;
}
2.字符串逆置
//字符串逆置(数组版)
void inverse(char* ch)
{
assert(ch);
int left = 0;
int right = strlen(ch) - 1;
while (left < right)
{
char temp = ch[left];
ch[left] = ch[right];
ch[right] = temp;
left++;
right--;
}
}
int main()
{
char ch[] = "123456";
inverse(ch);
printf("%s\n", ch);
return 0;
}
//字符串逆置(指针版)
void inverse(char* ch)
{
assert(ch);
char* ftemp = ch;
char* btemp = ch + strlen(ch) - 1;
while (ftemp < btemp)
{
char temp = *ftemp;
*ftemp = *btemp;
*btemp = temp;
ftemp++;
btemp--;
}
}
int main()
{
char ch[] = "123456";
inverse(ch);
printf("%s\n", ch);
return 0;
}
3.回文字符串
#include<stdio.h>
int Paind_Str(char* ch)
{
char* ftemp = ch;
char* btemp = ch + strlen(ch) - 1;
while (ftemp < btemp)
{
if (*ftemp != *btemp)
return 0;
ftemp++;
btemp--;
}
return 1;
}
int main()
{
char ch[] = "abcba";
int value = Paind_Str(ch);
if (value == 1)
printf("is Palindromic String\n");
else
printf("is not Palindromic String\n");
return 0;
}
内存函数:
memcpy
注意:
1.函数memcpy从src的位置开始向后复制n个字节的数据到dest的位置.
2.这个函数在遇到 '\0' 的时候并不会停下来.
3.如果src和dest有任何的重叠,复制的结果都是未定义的.
模拟实现memcpy:
//内存拷贝函数(memcpy)
//头文件:#include<string.h>
#include<assert.h>
void* my_memcpy(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7 };
int arr2[10] = { 0 };
my_memcpy(arr2, arr1, 28);
return 0;
}
memmove
注意:
1.memcpy不能对同一空间的数据进行拷贝,拷贝时会造成空间重叠覆盖
2.memcpy负责拷贝两块独立空间中的数据
3.memmove负责重叠内存的拷贝
模拟实现memmove
void* my_memmove(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
if (dest < src)
{
//从前向后
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
//从后向前
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr1 + 2, arr1, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
memset
#include<string.h>
int main()
{
char arr[] = "hello world";
memset(arr, 'x', 5);
printf("%s\n", arr);
return 0;
}
int main()
{
int arr[10] = { 0 };
memset(arr, 1, 40);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
因为memset是按照字节初始化的,因此不能将一个整型数组的内容全都初始化为1
memcmp
#include<string.h>
int main()
{
int arr1[] = { 1,2,3,4,5 };
int arr2[] = { 1,3,2 };
int ret = memcmp(arr1, arr2, 12);
printf("%d\n", ret);
return 0;
}
C语言编程训练:
1.猜名次
#include<stdio.h>
int main()
{
for (int a = 0; a <= 5; a++)
{
for (int b = 0; b <= 5; b++)
{
for (int c = 0; c <= 5; c++)
{
for (int d = 0; d <= 5; d++)
{
for (int e = 0; e <= 5; e++)
{
if ( (b == 2) + (a == 3) == 1
&& (b == 2) + (e == 4) == 1
&& (c == 1) + (d == 2) == 1
&& (c == 5) + (d == 3) == 1
&& (e == 4) + (a == 1) == 1)
{
if (a * b * c * d * e == 120)
{
printf("a=%d b=%d c=%d d=%d e=%d", a, b, c, d, e);
}
}
}
}
}
}
}
return 0;
}
2.猜凶手
#include<stdio.h>
int main()
{
int killer = 0;
for (killer = 'a'; killer <= 'd'; killer++)
{
if ((killer != 'a') + (killer == 'c')
+ (killer == 'd') + (killer != 'd') == 3)
{
printf("%c\n", killer);
}
}
return 0;
}
3.杨辉三角
int main()
{
int n = 0;
scanf("%d", &n);
int arr[30][30] = { 0 };
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
if (j == 0)
{
arr[i][j] = 1;
}
if (i == j)
{
arr[i][j] = 1;
}
if ((i >= 2) && (j >= 1))
{
arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
printf("%5d", arr[i][j]);
}
printf("\n");
}
return 0;
}
4.字符串左旋
//方法1
void LeftRotate(char arr[], int k)
{
int i = 0;
int len = strlen(arr);
k %= len;
for (i = 0; i < k; i++)
{
char tmp = arr[0];
int j = 0;
for (j = 0; j < len - 1; j++)
{
arr[j] = arr[j + 1];
}
arr[len - 1] = tmp;
}
}
int main()
{
char arr[] = "abcdef";
int k = 0;
scanf("%d", &k);
LeftRotate(arr, k);
printf("%s\n", arr);
return 0;
}
//方法二
void LeftRotate(char arr[], int k)
{
int len = strlen(arr);
k %= len;
char* tmp = (char*)malloc(len + 1);
int i = k;
int j = 0;
for (i = k; i < len; i++)
{
tmp[j++] = arr[i];
}
for (i = 0; i < k; i++)
{
tmp[j++] = arr[i];
}
for (i = 0; i < len; i++)
{
arr[i] = tmp[i];
}
free(tmp);
tmp = NULL;
}
int main()
{
char arr[] = "abcdef";
int k = 0;
scanf("%d", &k);
LeftRotate(arr, k);
printf("%s\n", arr);
return 0;
}
//方法三:
void reverse(char* left, char* right)
{
assert(left && right);
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
void LeftRotate(char arr[], int k)
{
int len = strlen(arr);
k %= len;
reverse(arr, arr + k - 1);
reverse(arr + k, arr + len - 1);
reverse(arr, arr + len - 1);
}
int main()
{
char arr[] = "abcdef";
int k = 0;
int len = strlen(arr);
scanf("%d", &k);
LeftRotate(arr, k);
printf("%s\n", arr);
return 0;
}
5.杨氏矩阵
//方法一:
struct Point
{
int x;
int y;
};
struct Point FindNum(int arr[3][3], int row, int col, int k)
{
int x = 0;
int y = col - 1;
struct Point p = { -1,-1 };
while (x <= row - 1 && y >= 0)
{
if (k < arr[x][y])
{
y--;
}
else if (k > arr[x][y])
{
x++;
}
else
{
p.x = x;
p.y = y;
return p;
}
}
return p;
}
int main()
{
int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };
int k = 0;
scanf("%d", &k);
struct Point ret = FindNum(arr, 3, 3, k);
printf("%d %d\n", ret.x, ret.y);
return 0;
}
//方法二:
bool FindNum(int arr[3][3], int row, int col, int k, int* px, int* py)
{
int a = 0;
int b = col - 1;
while (a <= row - 1 && b >= 0)
{
if (k < arr[a][b])
{
b--;
}
else if (k > arr[a][b])
{
a++;
}
else
{
*px = a;
*py = b;
return true;
}
}
*px = -1;
*py = -1;
return false;
}
int main()
{
int arr[3][3] = { {1,2,3},{4,5,6},{7,8,9} };
int k = 0;
int x = 0;
int y = 0;
scanf("%d", &k);
if (FindNum(arr, 3, 3, k, &x, &y))
{
printf("找到了\n");
printf("%d %d\n", x, y);
}
else
{
printf("找不到\n");
}
return 0;
}