Skip to content

Commit 674aa8f

Browse files
lzw-723ronreiter
authored andcommitted
Finished the Strings part.
1 parent ba1f522 commit 674aa8f

File tree

3 files changed

+122
-128
lines changed

3 files changed

+122
-128
lines changed

tutorials/learn-c.org/zh/Strings.md

Lines changed: 0 additions & 127 deletions
This file was deleted.

tutorials/learn-c.org/zh/Welcome.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ learn-c.org仍在建设中——如果你想贡献教程,请点击下方的`
1515
- [[数组]]
1616
- [[多维数组]]
1717
- [[条件语句]]
18-
- [[Strings]]
18+
- [[字符串]]
1919
- [[For loops]]
2020
- [[While loops]]
2121
- [[Functions]]

tutorials/learn-c.org/zh/字符串.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Tutorial
2+
--------
3+
4+
### 字符串的定义
5+
6+
C语言中的字符串实际上是字符数组。尽管使用指针是C语言中的高级课题,后面会详细解释,先使用指向字符数组的指针来定义简单的字符串,方法如下:
7+
8+
char * name = "John Smith";
9+
10+
这个方法创建了一个只能读取的字符串。
11+
如果希望定义一个可以被操作的字符串,需要定义为一个字符数组。
12+
13+
char name[] = "John Smith";
14+
15+
符号不同是因为它分配了一个数组变量,所以可以对它进行操作。方括号`[]`告诉编译器自动计算数组的大小。实际上是否指明大小都是一样的,字符串的长度为字符数组的大小减一。
16+
17+
char name[] = "John Smith";
18+
/* 与下面的意思相同 */
19+
char name[11] = "John Smith";
20+
21+
尽管字符串`John Smith`正好是10个字符长,但需要加一,是为了表明字符串的结束。一个特殊的char(等于0)表示字符串的结束。字符串的结尾被标记出来是因为程序不知道字符串的长度——只有编译器根据代码才知道。
22+
23+
### 用printf格式化字符串
24+
25+
可以使用`printf`方法将一个字符串与其他字符串一起格式化,具体方法如下:
26+
27+
char * name = "John Smith";
28+
int age = 27;
29+
30+
/* 打印'John Smith is 27 years old.' */
31+
printf("%s is %d years old.\n", name, age);
32+
33+
请注意,在打印字符串时,必须添加一个换行符(`\n`)来让下一个`printf`语句打印在一个新行中。
34+
35+
### 字符串长度
36+
37+
函数`strlen`返回作为参数传递的字符串的长度。
38+
39+
char * name = "Nikhil";
40+
printf("%d\n",strlen(name));
41+
42+
### 比较字符串
43+
44+
函数`strncmp`对两个字符串进行比较。如果相同,返回数字0;如果不同,返回其他数字。
45+
参数是要比较的两个字符串,以及最大比较长度。这个函数还有一个不安全的版本,叫做`strcmp`,不建议使用。例如:
46+
47+
char * name = "John";
48+
49+
if (strncmp(name, "John", 4) == 0) {
50+
printf("Hello, John!\n");
51+
} else {
52+
printf("You are not John. Go away.\n");
53+
}
54+
55+
### 字符串拼接
56+
57+
函数`strncat`将src字符串的前n个字符添加到目标字符串中,其中n为min(n,length(src))。
58+
传递的参数是目标字符串、源字符串和n——要增添的最大字符数。例如:
59+
60+
char dest[20]="Hello";
61+
char src[20]="World";
62+
strncat(dest,src,3);
63+
printf("%s\n",dest);
64+
strncat(dest,src,20);
65+
printf("%s\n",dest);
66+
67+
Exercise
68+
--------
69+
70+
使用指针定义字符串`first_name`,其值为`John`,使用字符数组定义字符串`last_name`,其值为`Doe`
71+
72+
Tutorial Code
73+
-------------
74+
75+
#include <stdio.h>
76+
#include <string.h>
77+
int main() {
78+
/* 定义first_name */
79+
/* 定义last_name */
80+
char name[100];
81+
82+
last_name[0] = 'B';
83+
sprintf(name, "%s %s", first_name, last_name);
84+
if (strncmp(name, "John Boe", 100) == 0) {
85+
printf("Done!\n");
86+
}
87+
name[0]='\0';
88+
strncat(name,first_name,4);
89+
strncat(name,last_name,20);
90+
printf("%s\n",name);
91+
return 0;
92+
}
93+
94+
95+
Expected Output
96+
---------------
97+
98+
Done!
99+
JohnBoe
100+
101+
Solution
102+
--------
103+
104+
#include <stdio.h>
105+
#include <string.h>
106+
int main() {
107+
char * first_name = "John";
108+
char last_name[] = "Doe";
109+
char name[100];
110+
111+
last_name[0] = 'B';
112+
sprintf(name, "%s %s", first_name, last_name);
113+
if (strncmp(name, "John Boe", 100) == 0) {
114+
printf("Done!\n");
115+
}
116+
name[0]='\0';
117+
strncat(name,first_name,4);
118+
strncat(name,last_name,20);
119+
printf("%s\n",name);
120+
return 0;
121+
}

0 commit comments

Comments
 (0)