2020.11 记录

C 语言: 用EOF代替回车作为终止输入的符号

EOF 知识链接:

What is EOF in the C programming language?

用EOF终止输入需要保证EOF在回车后输入, 否则EOF不会被识别到

1
2
3
4
5
// 用EOF终止字符的输入
if ( (str = getchar()) != EOF ) // 继续输入
{
/*语句*/
}else /*终止语句*/

题目:

http://oj.kfcoding.com/contest/24/problem/6-1

答案:

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
#include <stdio.h>
#include <string.h>

void StringCount(char s[])
{
int English_char = 0, Space_and_Enter = 0,
num_char = 0, other_char = 0;

for (int i = 0; i < 100; i++)
{
if ((s[i] = getchar()) != EOF)
{
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
English_char++;
else if (s[i] >= '0' && s[i] <= '9')
num_char++;
else if (s[i] == ' ' || s[i] == '\n')
Space_and_Enter++;
}else break;
}
printf("%d %d %d %d", English_char, Space_and_Enter, num_char
, strlen(s) - English_char - Space_and_Enter - num_char - 1);
}

int main()
{
char str[100];

StringCount(str);

return 0;
}

C语言: 消除输出的最后一位空格

1
printf("%c",8);

上面这里是输出ASCII码为8的字符,这是一个Backspace控制符。于是可以往回消去一个字符。

PS: 经过验证, 该方法有效, 但是在学校的oj平台上无用, 且会被判定为未知字符.

Git & Github

How to clone all remote branches in Git?

VSCODE: 输出中文乱码解决方法

https://www.w3xue.com/exp/article/20203/80007.html

https://zhuanlan.zhihu.com/p/30127242)

C语言: strrev() function

strrev() function

It is used to reverse the given string expression.

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>

int main()
{
char s1[50];

printf("Enter your string: ");
gets(s1);
printf("\nYour reverse string is: %s",strrev(s1));
return(0);
}
1
2
3
// output
Enter your string: studytonight
Your reverse string is: thginotyduts

C语言: scanf输出输出一行包括空格的字符串

String Input and Output

Input function scanf() can be used with %s format specifier to read a string input from the terminal. But there is one problem with scanf() function, it terminates its input on the first white space it encounters. Therefore if you try to read an input string “Hello World” using scanf() function, it will only read Hello and terminate after encountering white spaces.

However, C supports a format specification known as the edit set conversion code %[..] that can be used to read a line containing a variety of characters, including white spaces.

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
#include<string.h>

void main()
{
char str[20];
printf("Enter a string");
scanf("%[^\n]", &str); //scanning the whole string, including the white spaces
printf("%s", str);
}

C语言: 动态分配数组(一维)

数组元素个数为变量

1
2
3
4
5
6
7
8
int *a = NULL;								// 声明数组头指针	
int N = 0; // 声明变量并初始化
scanf("%d", &N); // 变量赋值
a = (int *) malloc(N * sizeof(int)); // 动态分配内存
/*
之后a的用法与a[N]无异
*/
free(a); //!!!i使用结束后必须释放内存空间

C语言: 动态分配数组(二维)

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
#include <stdio.h>
#include <stdlib.h>

int main()
{
int nrows, ncolumns;
int i = 0;

scanf("%d %d", nrows, ncolumns);

// 为数组分配行数 注意分配的是整形指针的数量 每行元素是个整形指针
int **array = malloc(nrows * sizeof(*array));

// 对每行分配数组的个数(二维数组列数)
for(i = 0; i < nrows; i++)
{
// 为每行申请内存空间
array[i] = (int *)malloc(ncolumns * sizeof(int));
}

/*接下来等于对 a[nrows][ncolumns] 进行操作
*/

free(array); // 一定要在最后讲内存空间释放!!

return 0;
}

C语言: Problem with scanf() when there is fgets()/gets()/scanf() after it

article_links

C语言: fgets函数的一些注意事项

1
char *fgets(char *str, int n, FILE *stream);
  1. fets函数中的第二个参数为字符串的允许输入个数, 实际上这个个数是要算上字符串的结尾‘\0’, 除去‘\0’, 有效输入字符为 n-1个.

  2. 当fgets函数以换行符号作为最后的终止符, 如果当前输入的字符个数小于n-1, 那么换行符\n也会作为输入字符记载到字符串中.