Character Pointer Scrolling Record
1. The following description of "pointer" is incorrect:
2. The following code description is correct:
char* p = "hello beauty!"; const char* p = "hello beauty!";//More appropriate writing
3. The correct description of the array pointer is:
4. Which of the following is the array pointer:
The array pointer is a pointer to the array
Pointer array is an array, which stores pointers
5. Which of the following codes is wrong?
int main() { int *p = NULL; int arr[10] = {0};//arr is the array name, and the array name is the address int of the first element* //int* p = arr; return 0; } A.p = arr; B.int (*ptr)[10] = &arr;//Pointer array C.p = &arr[0];//Equivalent to A D.p = &arr;//Take the array address and put it into the integer pointer. The two do not match
6. The following code describes the array name incorrectly:
int main() { int arr[10] = {0}; return 0; } A.Array name arr and&arr It's the same//&The values of arr and arr are the same, but their meanings are different B.sizeof(arr),arr Represents the entire array C.&arr,arr Represents the entire array D.except sizeof(arr)and&arr Array name in, array name in other places arr,Are the addresses of the first element of the array.
7. Define array:
//How to define an int pointer array with 10 array elements: int * arr[10]; A.int a[10]//integer array B.int (*a)[10]//Array pointer C.int *a[10]; D.int (*a[10])(int);//Function pointer array
8. The execution result of the following code is
int main() { char str1[] = "hello bit."; char str2[] = "hello bit."; char *str3 = "hello bit."; char *str4 = "hello bit."; if(str1 == str2) printf("str1 and str2 are same\n"); else printf("str1 and str2 are not same\n");//Print if(str3 == str4) printf("str3 and str4 are same\n");//Print else printf("str3 and str4 are not same\n"); return 0; }
9. Young matrix
There is a number matrix. Each row of the matrix is increasing from left to right, and the matrix is increasing from top to bottom. Please write a program to find whether a number exists in such a matrix.
Requirement: time complexity is less than O(N) ---- avoid traversing data
1 2 3
4 5 6
7 8 9
Thought expansion
code implementation
void find_num_in_young (int arr[3][3],int k,int *px,int *py) { int i = 0; int j =*py - 1; int flag = 0; while (i <=* px - 1 && j >=0) { if (arr[i][j] < k) i++; else if (arr[i][j] > k) j--; else { //eureka flag = 1; *px = i; *py = j; break; } } if (flag == 0) { *px = -1; *py = -1; } }
Principal function
int main() { int arr[3][3] = { 1,2,3,4,5,6,7,8,9 }; int k=0; scanf("%d", &k);//Enter Search Number //Traversal array int x = 0; int y = 3; //Return type parameter find_num_in_young(arr, k, &x, &y); if (x == 1 && y == -1) printf("can't find\n"); else printf("Yes, the subscript is:"); return 0; }
10. Left rotation of string
Implement a function that can rotate k characters in a left-handed string.
Code implementation 1
Disadvantages: Some codes will move repeatedly
ABCD Rotate a character to the left to get BCDA
ABCD Rotate left two characters to get CDAB
void left_move(char arr[], int k) { //Rotate only one character at a time, and execute this action k times int i = 0; int len = strlen(arr); k %= len;//Improve efficiency and keep cycle times in single digits for (i = 0; i < k; i++) { //1 char tmp = arr[0]; //2 Move all the characters behind one position forward int j = 0; for (j = 0; j < len - 1; j++) arr[j] = arr[j + 1]; //3 arr[len - 1] = tmp; } }
Code implementation 2:
One code is exchanged only twice
void reverse(char* left, char* right) { assert(left && right); while (left < right) { char tmp = *left; *left = *right; *right = tmp; left++; right--; } } void left_move(char arr[], int k) { //Rotate only one character at a time, and execute this action k times int i = 0; int len = strlen(arr); k %= len; reverse(arr, arr + k); reverse(arr+k,arr+len-1); reverse(arr, arr + len - 1); }
Main function:
int main() { char arr[] = "abcdefghi"; int k = 0; scanf("%d", &k); left_move(arr, k); printf("%s\n", arr); return 0; }
11. String rotation result
Write a function to determine whether one string is the string after another string is rotated.
For example, given s1=AABCD and s2=BCDAA, 1 is returned
Given s1=abcd and s2=ACBD, 0 is returned
AABCD Rotate a character to the left to get ABCDA
AABCD Rotate two characters left to get BCDAA
AABCD Rotate a character to the right to get DAABC
Code implementation 1
int is_left_move(char arr1[], char arr2[]) { int len = strlen(arr1); int i = 0; for (i = 0; i < len; i++) { char tmp = arr1[0]; int j = 0; for (j = 0; j < len - 1; j++) arr1[j] = arr1[j + 1]; arr1[len - 1] = tmp; if (strcmp(arr1, arr2) == 0) return 1; } return 0; }
Code Implementation 2
int is_left_move(char arr1[], char arr2[]) { //strcat strstr strncat int len1 = strlen(arr1); int len2 = strlen(arr2); if (len1 != len2) return 0; strncat(arr1, arr1, len1); char* ret = strstr(arr1, arr2); if (ret == NULL) return 0; else return 1; }
main function
//Write a function to determine whether a string is a string after rotation of another string int main() { char arr1[] = "ABCDEF"; //char arr1[20] = "ABCDEF";// Note: Modern code 2 is implemented on the premise that the arr1 space needs to be large enough char arr2[] = "CDEFAB"; int ret = is_left_move(arr1, arr2);//Judge whether arr2 is obtained by rotation of arr1 if (1 == ret) printf("Yes\n"); else printf("No\n"); return 0; }
``c
//Write a function to determine whether a string is a string after rotation of another string
int main()
{
char arr1[] = "ABCDEF";
//char arr1[20] = “ABCDEF”;// Note: Modern code 2 is implemented on the premise that the arr1 space needs to be large enough
char arr2[] = "CDEFAB";
int ret = is_left_move(arr1, arr2);// Judge whether arr2 is obtained by rotation of arr1
if (1 == ret)
printf("Yes\n");
else
printf("No\n");
return 0;
}