Initial commit: C language learning code
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !单字符的输入输出
|
||||
/*
|
||||
1.putchar
|
||||
int putchar(int c);
|
||||
向标准输出写一个字符
|
||||
返回写了几个字符,EOF(-1)表示写失败
|
||||
2.getchar
|
||||
int getchar(void);
|
||||
从标准输入读入一个字符
|
||||
返回类型是int是为了返回EOF (-1)
|
||||
Windows->Ctrl-Z
|
||||
Unix—>Ctrl-D
|
||||
3.
|
||||
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int ch;
|
||||
|
||||
while ((ch=getchar())!=EOF)
|
||||
{
|
||||
putchar(ch);
|
||||
}
|
||||
|
||||
printf("EOF\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !
|
||||
/*
|
||||
1.字符串
|
||||
以0(整数0)结尾的一串字符
|
||||
0或'\0'是一样的,但是和'0'不同
|
||||
O标志字符串的结束,但它不是字符串的一部分
|
||||
计算字符串长度的时候不包含这个0
|
||||
字符串以数组的形式存在,以数组或指针的形式访问
|
||||
更多的是以指针的形式
|
||||
string.h 里有很多处理字符串的函数
|
||||
2.字符串常量
|
||||
“Hello”
|
||||
"He11o”会被编译器变成一个字符数组放在某处,这个数组的长度是6,结尾还有表示结束的0
|
||||
两个相邻的字符串常量会被自动连接起来
|
||||
3.字符串
|
||||
C语言的字符串是以字符数组的形态存在的
|
||||
不能用运算符对字符串做运算
|
||||
通过数组的方式可以遍历字符串
|
||||
唯一特殊的地方是字符串字面量可以用来初始化字符数组
|
||||
以及标准库提供了一系列字符串函数
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* 1.字符串的定义 */
|
||||
char word[] = {'H', 'e', 'l', 'l', 'l', 'o', '\0'};
|
||||
|
||||
/* 2.字符串变量 */
|
||||
char *p = "Hello";
|
||||
char word2[] = "Hello";
|
||||
char line[10] = "Hello";
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串函数string.h
|
||||
/*
|
||||
1.string.h
|
||||
strlen、strcmp、strcpy、strcat、strchr、strstr
|
||||
2.strlen
|
||||
size_t strlen(const char *s);
|
||||
返回s的字符串长度(不包括结尾的0)
|
||||
3.strcmp
|
||||
int strcmp(const char *s1, const char *s2); 比较两个字符串,返回:
|
||||
0:s1==S2
|
||||
1:s1>s2
|
||||
-1:s1<s2
|
||||
4.strcpy
|
||||
char * strcpy(char *restrict dst, const char *restrict src);
|
||||
把src的字符串拷贝到dst
|
||||
restrict表明src和dst不重叠(C99)
|
||||
返回dst 为了能链起代码来
|
||||
* 复制一个字符串
|
||||
* char *dst =(char*)malloc(strlen(src)+1);
|
||||
* strcpy(dst, src);
|
||||
5.字符串中找字符
|
||||
char*strchr(const char *s, int c);
|
||||
char*strrchr(const char *s, int c);
|
||||
返回NULL表示没有找到
|
||||
如何寻找第2个?
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char s[]="hello";
|
||||
char *p1 = strchr( s, 'l' );
|
||||
printf("%s\n", p1);
|
||||
p1 = strchr(p1+1, 'l' ); // !找到p1的下一个
|
||||
printf("%s\n", p1);
|
||||
|
||||
char *p2 = strrchr( s, 'l' );
|
||||
printf("%s\n", p2);
|
||||
|
||||
char *t = (char *)malloc(strlen(p1)+1);
|
||||
printf("%s\n", t);
|
||||
free(t);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串函数string.h
|
||||
/*
|
||||
1.string.h
|
||||
strlen、strcmp、strcpy、strcat、strchr、strstr
|
||||
2.strlen
|
||||
size_t strlen(const char *s);
|
||||
返回s的字符串长度(不包括结尾的0)
|
||||
3.strcmp
|
||||
int strcmp(const char *s1, const char *s2); 比较两个字符串,返回:
|
||||
0:s1==S2
|
||||
1:s1>s2
|
||||
-1:s1<s2
|
||||
4.strcpy
|
||||
char * strcpy(char *restrict dst, const char *restrict src);
|
||||
把src的字符串拷贝到dst
|
||||
restrict表明src和dst不重叠(C99)
|
||||
返回dst 为了能链起代码来
|
||||
* 复制一个字符串
|
||||
* char *dst =(char*)malloc(strlen(src)+1);
|
||||
* strcpy(dst, src);
|
||||
5.字符串中找字符
|
||||
char*strchr(const char *s, int c);
|
||||
char*strrchr(const char *s, int c);
|
||||
返回NULL表示没有找到
|
||||
如何寻找第2个?
|
||||
6.字符串中找字符串
|
||||
char * strstr(const char *s1, const char *s2);
|
||||
char * strcasestr(const char *s1, const char *s2);
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串函数string.h
|
||||
/*
|
||||
1.string.h
|
||||
strlen、strcmp、strcpy、strcat、strchr、strstr
|
||||
2.strlen
|
||||
size_t strlen(const char *s);
|
||||
返回s的字符串长度(不包括结尾的0)
|
||||
3.strcmp
|
||||
int strcmp(const char *s1, const char *s2); 比较两个字符串,返回:
|
||||
0:s1==S2
|
||||
1:s1>s2
|
||||
-1:s1<s2
|
||||
*/
|
||||
|
||||
int my_strcmp(const char *s1, const char *s2)
|
||||
{
|
||||
int idx = 0;
|
||||
while (s1[idx]!='\0' && s1[idx] == s2[idx])
|
||||
{
|
||||
idx++;
|
||||
}
|
||||
return s1[idx] - s2[idx];
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* strcmp */
|
||||
char s1[] = "abc";
|
||||
char s2[] = "abc";
|
||||
printf("strcmp=%d\n", strcmp(s1, s2));
|
||||
|
||||
char s3[] = "abc";
|
||||
char s4[] = "bbc";
|
||||
printf("strcmp=%d\n", strcmp(s3, s4));
|
||||
|
||||
char s5[] = "abc";
|
||||
char s6[] = "Abc";
|
||||
printf("strcmp=%d\n", strcmp(s5, s6));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串函数string.h
|
||||
/*
|
||||
1.string.h
|
||||
strlen、strcmp、strcpy、strcat、strchr、strstr
|
||||
2.strlen
|
||||
size_t strlen(const char *s);
|
||||
返回s的字符串长度(不包括结尾的0)
|
||||
3.strcmp
|
||||
int strcmp(const char *s1, const char *s2); 比较两个字符串,返回:
|
||||
0:s1==S2
|
||||
1:s1>s2
|
||||
-1:s1<s2
|
||||
4.strcpy
|
||||
char * strcpy(char *restrict dst, const char *restrict src);
|
||||
把src的字符串拷贝到dst
|
||||
restrict表明src和dst不重叠(C99)
|
||||
返回dst 为了能链起代码来
|
||||
* 复制一个字符串
|
||||
* char *dst =(char*)malloc(strlen(src)+1);
|
||||
* strcpy(dst, src);
|
||||
*/
|
||||
|
||||
int my_strcpy(char *restrict dst, const char *restrict src)
|
||||
{
|
||||
int idx = 0;
|
||||
while (src[idx]!='\0')
|
||||
{
|
||||
dst[idx]=src[idx];
|
||||
idx++;
|
||||
}
|
||||
dst[idx]='\0';
|
||||
return dst;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* strcpy */
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串函数string.h
|
||||
/*
|
||||
1.string.h
|
||||
strlen、strcmp、strcpy、strcat、strchr、strstr
|
||||
2.strlen
|
||||
size_t strlen(const char *s);
|
||||
返回s的字符串长度(不包括结尾的0)
|
||||
3.
|
||||
*/
|
||||
|
||||
int my_strlen(const char *s)
|
||||
{
|
||||
int idx = 0;
|
||||
while (s[idx]!='\0')
|
||||
{
|
||||
idx++;
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* strlen */
|
||||
char line[] = "Hello";
|
||||
printf("strlen=%lu\n", strlen(line));
|
||||
printf("my_strlen=%d\n", my_strlen(line));
|
||||
printf("sizeof=%lu\n", sizeof(line));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !
|
||||
/*
|
||||
1.字符串常量
|
||||
char*s = "Hello, world!";
|
||||
s是一个指针,初始化为指向一个字符串常量
|
||||
由于这个常量所在的地方,所以实际上s是const char*s,但是由于历史的原因,编译器接受不带const的写法
|
||||
但是试图对s所指的字符串做写入会导致严重的后果
|
||||
如果需要修改字符串,应该用数组:
|
||||
char s[] = "Hello, world!";
|
||||
2.指针还是数组?
|
||||
char *str ="Hello"
|
||||
char word[]="Hello"
|
||||
数组:这个字符串在这里
|
||||
作为本地变量空间自动被回收
|
||||
指针:这个字符串不知道在哪里
|
||||
处理参数
|
||||
动态分配空间
|
||||
3.char*是字符串?
|
||||
字符串可以表达为char*的形式
|
||||
char*不一定是字符串
|
||||
本意是指向字符的指针,可能指向的是字符的数组(就像int*一样)
|
||||
只有它所指的字符数组有结尾的0,才能说它所指的是字符串
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* 1.字符串的定义 */
|
||||
char *s="Hello World!";
|
||||
char *s2 = "Hello World!";
|
||||
char s3[] = "Hello World!";
|
||||
s[0] = 'B';
|
||||
|
||||
printf("s =%p\n", s );
|
||||
printf("s2=%p\n", s2);
|
||||
printf("s3=%p\n", s3);
|
||||
s3[0] = 'B';
|
||||
printf("Here!s3[0]=%c\n", s3[0]);
|
||||
printf("Here!s[0]=%c\n", s[0]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串数组
|
||||
/*
|
||||
1.字符串数组
|
||||
char* *a
|
||||
a是一个指针,指向另一个指针,那个指针指向一个字符(串)
|
||||
char a[][]
|
||||
2.程序参数
|
||||
int main(int argc, char const *argv[])
|
||||
argv[0]是命令本身
|
||||
当使用Unix的符号链接时,反映符号链接的名字
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char a[][10] = {"Hello",
|
||||
"World",
|
||||
"C",
|
||||
"Language"};
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
printf("%s ", a[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
/* a[0] --> char* */
|
||||
char *b[] = {"Hello",
|
||||
"World",
|
||||
"C",
|
||||
"Language"};
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
printf("%s ", b[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串数组
|
||||
/*
|
||||
1.字符串数组
|
||||
char* *a
|
||||
a是一个指针,指向另一个指针,那个指针指向一个字符(串)
|
||||
char a[][]
|
||||
2.程序参数
|
||||
int main(int argc, char const *argv[])
|
||||
argv[0]是命令本身
|
||||
当使用Unix的符号链接时,反映符号链接的名字
|
||||
*/
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < argc; i++)
|
||||
{
|
||||
printf("%d:%s\n", i, argv[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串的输入输出
|
||||
/*
|
||||
1.字符串赋值?
|
||||
char *t =“title”
|
||||
char *s;
|
||||
s=t;
|
||||
并没有产生新的字符串,只是让指针s指向了t所指的字符串,对s的任何操作就是对t做的
|
||||
2.字符串输入输出
|
||||
char string[8];
|
||||
scanf("%s",string);
|
||||
printf("%s",string);
|
||||
scanf读入一个单词(到空格、tab或回车为止)
|
||||
scanf是不安全的,因为不知道要读入的内容的长度
|
||||
3.安全的输入
|
||||
char string[8];
|
||||
scanf("%7s", string);
|
||||
在%和s之间的数字表示最多允许读入的字符的数量,这个数字应该比数组的大小小一
|
||||
下一次scanf从哪里开始?
|
||||
4.常见错误
|
||||
char *string;
|
||||
scanf("%s", string);
|
||||
以为char*是字符串类型,定义了一个字符串类型的
|
||||
变量string就可以直接使用了
|
||||
由于没有对string初始化为0,所以不一定每次运行都出错
|
||||
5.空字符串
|
||||
char buffer[100]="";
|
||||
这是一个空的字符串,buffer[0]=='\0'
|
||||
char buffer[]="";
|
||||
这个数组的长度只有1!
|
||||
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char word1[8];
|
||||
char word2[8];
|
||||
scanf("%7s", word1); // 读入一个单词
|
||||
scanf("%7s", word2); //%7s表示最多读入7个字符,最后一个字符是\0,防止越界
|
||||
printf("%s##%s##\n", word1, word2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串数组
|
||||
/*
|
||||
1.字符串数组
|
||||
char* *a
|
||||
a是一个指针,指向另一个指针,那个指针指向一个字符(串)
|
||||
char a[][]
|
||||
2.程序参数
|
||||
int main(int argc, char const *argv[])
|
||||
argv[0]是命令本身
|
||||
当使用Unix的符号链接时,反映符号链接的名字
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
char str[100];
|
||||
printf("请输入一个字符串:");
|
||||
scanf("%s", str);
|
||||
|
||||
int len=strlen(str); // !strlen()函数返回字符串的长度,不包括'\0'字符
|
||||
int is_huiwenzi = 1;
|
||||
|
||||
for (int i = 0; i < len / 2;i++)
|
||||
{
|
||||
if (str[i]!=str[len-i-1])
|
||||
{
|
||||
is_huiwenzi = 0;
|
||||
}
|
||||
}
|
||||
if (is_huiwenzi)
|
||||
{
|
||||
printf("是回文字\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("不是回文字\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user