-
Notifications
You must be signed in to change notification settings - Fork 0
/
字符串与字符数组
74 lines (58 loc) · 1.58 KB
/
字符串与字符数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int GersStrLength(char[]);
void GetString(char[],int count);
int GersStrLength(char str[])
{
int count = 0;//字符串中的字符个数
while(str[count] != '\0')
{
/*
if(str[count] == '\n')
{
str[count] = '\0';
break;
}
*/
count++;
}
return count;
}
void GetString(char str[],int count)
{
//使用fgets函数接收字符串,使用\0替换字符数组的最后一位\n
fgets(str,count,stdin);
//查找\n
char * find = strchr(str,'\n');
if(find)//如果找到了
*find = '\0';
}
int main()
{
char names1[] = {'j','a','c','k','\0'};
//fgets(names1,5,stdin);
GetString(names1,20);
int len = GersStrLength(names1);
printf("字符串的长度为:%d\n",len);
/*
//定义字符串的几种方式
//字符串和字符数组的区别:最后一位是否是空字符
char names1[] = {'j','a','c','k','\0'};
char names2[] = "jack";
char names3[50];
int i;
printf("请输入新名称:");
//gets(names3);//等价于scanf("%s",names3);只能录入字符串
//puts(names3);//等价于printf("s\n",names3);只能输出字符串
fgets(names3,50,stdin);//从标准输入流中读取10字节到数组names3中
printf("数组names3中的内容为:\n");
for(i = 0;i < 50;i++)
{
printf("%d\n",names3[i]);
}
printf("数组所占空间大小:%d\n",sizeof(names1));
printf("数组所占空间大小:%d\n",sizeof(names2));
*/
return 0;
}