C language elementary string reverse bubble sort binary print odd and even digits

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right


Preface

The most important things in C language are functions, arrays, and pointers. Pointers are the core things in C language, and they are also the most unfriendly to newbies. I learned pointers for the first time and found that pointers are more difficult to avoid. No need to.

One, function

Classification of functions in C language:

1. Library functions

The library function is the compiled function, we only need to call it, but when the library function is in use, the header file corresponding to #include must be included.

2. Custom functions

A custom function is a function written by yourself, because some code segments are frequently called in the program. In order to reduce the duplication of the code segment, and to improve the utilization rate of the code segment, these frequently used functions will be written into a function and put Outside the main program, it is convenient for every call. But remember to declare it before using it.

3. Actual participation parameter

The parameters actually passed to the function are called actual parameters. The actual parameters can be: constants, variables, expressions, functions, etc. No matter what kind of quantity the actual parameters are, they must have certain values ​​when the function is called in order to transfer these values ​​to the formal parameters.

Formal parameters refer to the variables in parentheses after the function name. Because formal parameters are only instantiated (allocated memory units) during the function being called, they are called formal parameters. The formal parameters are automatically destroyed when the function call is completed. Therefore, formal parameters are only valid in functions.

Note: The changes to the formal parameters will not affect the actual parameters. Here is an example of the simplest code:

#include<stdio.h>

//void Swap(int *a, int *b)
//{
    
    
//	int tmp = *a;
//	*a = *b;
//	*b = tmp;
//}
void Swap(int a, int b)
{
    
    
	int tmp = a;
	a = b;
	b = tmp;
}
void main()
{
    
    
	//实际参数
	int a = 100;
	int b = 200;
	printf("Swap before a = %d, b = %d\n", a, b);
	Swap(&a, &b);
	printf("Swap After a = %d, b = %d\n", a, b);
}

After the function is called, the values ​​of the two have not changed. The commented-out function can achieve this function. It uses the change address to realize the conversion between the two numbers.

4. Nested calls and recursive functions

Nested calls are nested calls that can be made between functions and functions, but note that if a function is not declared yet, it is not possible to call inside another function, so write those functions and put these functions in advance To make a unified statement, this is a good habit and must be cultivated.

The programming technique of a recursive function program calling itself is called recursion. Recursion is widely used as an algorithm in programming languages. A process or function has a method of directly or indirectly calling itself in its definition or description. It usually converts a large and complex problem into a smaller problem similar to the original problem to solve. The recursive strategy only A small amount of programs can be used to describe multiple repetitive calculations required in the process of solving problems, which greatly reduces the amount of program code. The main way of thinking about recursion is to make things smaller
. There are two necessary conditions for recursion.
There are restrictions. When these restrictions are met, the recursion will not continue.
After each recursive call, it gets closer and closer to this restriction.
Example: Calculate Fibonacci number 1 1 2 3 5 8

#include<stdio.h>
size_t Fib(int n)  //递归方式
{
    
    
	if (n <= 2)
		return 1;
	else
		return Fib(n - 1) + Fib(n - 2);
}
size_t Fib1(int n)  //数组赋值方式
{
    
    
	if (n <= 2)
		return 1;
	int fib1 = 1;
	int fib2 = 1;
	int fib;
	//迭代  杜绝栈的溢出
	for (int i = 3; i <= n; ++i)
	{
    
    
		fib = fib1 + fib2;
		fib1 = fib2;
		fib2 = fib;
	}
	return fib;
}
void main(){
    
    
	int n;
	scanf("%d", &n);
	size_t result = Fib(n);
	printf("result = %u\n", result);
	size_t result1 = Fib1(n);
	printf("result = %u\n", result1);
}

Two methods are used here, recursive and non-recursive (without considering overflow issues)

Example 2: Write a function reverse_string(char * string) (recursive implementation)
string reverse order

#include<stdio.h>
#include<string.h>
void reverse(char *string)//数组作为函数参数的时候,会退化为指针。
{
    
    
	int len = strlen(string);//求出字符串的长度
	if (*string) //不为空
	{
    
    
		char temp = *string;
		*string = *(string + len - 1);  //将最后一个字符给第一个,第一个保存在temp中,后面再赋值给最后一个
		*(string + len - 1) = '\0';
		reverse(string + 1); //递归的方式
		*(string + len - 1) = temp;
	}else
		return;
}
int main()
{
    
    
	char s[10] = "abcde";
	reverse(s);
	printf("%s\n", s);
	return 0;
}

Second, the array

An array is a collection of elements of the same type.
Example: To implement a bubble sort of an integer array

#include<stdio.h>
void main(){
    
    
	int arr[] = {
    
     0, 1, 2, 6, 4, 8, 12, 3 };
	int n = sizeof(arr) / sizeof(arr[0]);
	for (int i = 0; i<n - 1; ++i) //用于控制比较的趟数
	{
    
    
		for (int j = 0; j<n - i - 1; ++j) //用于控制每一趟的排序
		{
    
    
			if (arr[j] > arr[j + 1])
			{
    
    
				//交换两个数
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
			}
		}
	}
	for (int k = 0; k < n; k++){
    
    
		printf("%d\t", arr[k]);
	}
}

Three, pointer

In computer science, a pointer (Pointer) is an object in a programming language. Using an address, its value directly points to a value stored in another place in the computer memory. Since the required variable unit can be found through the address, it can be said that the address points to the variable unit. Therefore, the visualized address is called "pointer".
A pointer is a variable, a variable used to store an address. (The value stored in the pointer is treated as an address).

1. Pointers and pointer types

The type of the pointer determines how much authority (can manipulate several bytes) when dereferencing the pointer. For example: the
pointer dereference of char* can only access one byte, while the dereference of the pointer of int* can access four bytes.
Wild pointer means that the position the pointer points to is unknowable (random, incorrect, and not explicitly restricted)

2. Pointer operations

Only pointer-pointer
Example: You can find the length of a string by subtracting pointers

#include<stdio.h>
//指针相减
void main()
{
    
    
	char ar[5] = {
    
     'a', 'b', 'c', 'd', 'e' };
	char *end = &ar[5];
	char *start = &ar[0];

	int len = end - start;
	printf("len = %d\n", len);
}

3. Pointer array

An array of pointers is an array of pointers

to sum up

In addition to functions, arrays, and pointers, there is also an operator that is also more important. These are all nested and used. You don’t need to memorize it carefully. You will be familiar with it by writing more code. Finally, an odd number of binary output is given. And even-numbered examples;

//获取一个整数二进制序列中所有的偶数位和奇数位,分别打印出二进制序列
#include<stdio.h>
void main(){
    
    
	int n;
	scanf("%d", &n);
	int ar[32] = {
    
     0 };//定义一个数组,来存放给出数字的全部二进制位
	for (int i = 0; i < 32; i+=2){
    
    
		if (((n >> i) & 1) == 1)
			ar[31 - i] = 1;
	}
	for (int j = 1; j < 32; j+=2){
    
    
		if (((n >> j) & 1) == 1)
			ar[31 - j] = 1;
	}
			printf("偶数位为:");
			for (int i = 0; i <31; i += 2){
    
    
				printf("%d", ar[i]);
			}
			printf("\n");
			printf("奇数位为:");
			for (int i = 1; i <32; i += 2){
    
    
				printf("%d", ar[i]);
			}
			printf("\n");
}

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/109653211