文章目录
- 一、题目: 编写一个函数,使用指针交换两个整数变量的值。
- 二、题目: 编写一个函数,计算整型数组中所有元素的总和,并通过指针返回结果。
- 三、题目: 编写一个函数,查找整型数组中的最大值,并通过指针返回该值及其索引
- 四、题目: 编写一个函数,使用指针将输入的字符串进行反转。
- 五、题目: 编写一个函数,统计输入字符串中的单词个数,并通过指针返回结果。
- 六、题目: 编写一个函数,删除字符串中指定的字符,并通过指针修改原始字符串。
- 七、题目: 编写一个函数,实现字符串复制功能,即将一个字符串复制到另一个字符数组中。
- 八、题目: 编写一个函数,判断一个字符串是否是另一个字符串的子串,并返回子串的起始位置。
- 九、题目: 给定一个单链表,编写一个函数逆序输出链表中的所有元素。
- 十、题目: 编写一个程序,动态分配一个整型数组,并释放这块内存空间(直接写在main里)。
- 十一、题目: 编写一个函数,计算整型数组中所有元素的平均值,并通过指针返回结果。
- 十二、题目: 编写一个函数,统计整型数组中指定值出现的次数,并通过指针返回结果。
- 十三、题目: 编写一个函数,交换输入字符串中的两个单词,并通过指针修改原始字符串
- 十四、题目: 编写一个函数,计算输入字符串的长度,并通过指针返回结果。
- 十五、题目: 编写一个函数,将输入字符串中的所有小写字母转换为大写,并通过指针修改原始字符串。
- 十六、题目: 编写一个函数,判断输入字符串是否是回文,并通过指针返回结果(1表示是,0表示否)。
- 十七、题目: 编写一个函数,交换二维整型数组中指定的两行或两列,并通过指针修改原始数组。
- 十八、题目: 编写一个函数,按指定格式打印二维整型数组的内容。
- 十九、题目: 编写一个程序,动态分配一个二维整型数组,并释放这块内存空间(直接写在main里)。
- 二十、题目: 编写一个函数,将一个一维整型数组复制到另一个数组,并通过指针修改原始数组。
一、题目: 编写一个函数,使用指针交换两个整数变量的值。
方法一
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
int t = 0;
t = *a;
*a = *b;
*b = t;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
方法二
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
二、题目: 编写一个函数,计算整型数组中所有元素的总和,并通过指针返回结果。
#include <iostream>
using namespace std;
void array_sum(int* arr, int size, int* sum) {
*sum = 0;
for (int i = 0; i < size; i++) {
*sum = *sum + arr[i];
}
}
int main() {
int arr[] = { 1, 2, 3, 4, 5 };
int size = sizeof(arr) / sizeof(arr[0]);
int sum;
array_sum(arr, size, &sum);
printf("Sum = %d\n", sum);
return 0;
}
三、题目: 编写一个函数,查找整型数组中的最大值,并通过指针返回该值及其索引
#include <iostream>
using namespace std;
void find_max(int* arr, int size, int* max_value, int* index)
{
*max_value = arr[0];
for (int i = 0; i < size; i++) {
if (arr[i] > *max_value) {
*max_value = arr[i];
*index = i;
}
}
}
int main() {
int arr[] = { 3, 1, 4, 1, 5, 9 };
int size = sizeof(arr) / sizeof(arr[0]);
int max_value, index;
find_max(arr, size, &max_value, &index);
printf("Max value = %d, Index = %d\n", max_value, index);
return 0;
}
四、题目: 编写一个函数,使用指针将输入的字符串进行反转。
#include <iostream>
#include<string.h>
using namespace std;
void reverse_string(char* str) {
int len = strlen(str);
int tmp=0;
for (int i = 0; i < len/2;i++) {
tmp = str[i];
str[i] = str[len -i- 1];
str[len - i - 1] = tmp;
}
}
int main() {
char str[] = "hello";
reverse_string(str);
printf("Reversed string = %s\n", str);
return 0;
}
五、题目: 编写一个函数,统计输入字符串中的单词个数,并通过指针返回结果。
#include <iostream>
#include <ctype.h>
using namespace std;
int count_words(const char* str) {
int num = 1;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ' ') {
num++;
}
}
return num;
}
int main() {
const char* sentence = "Hello world, this is C programming.";
int word_count = count_words(sentence);
printf("Word count = %d\n", word_count);
return 0;
}
六、题目: 编写一个函数,删除字符串中指定的字符,并通过指针修改原始字符串。
#include <iostream>
#include <vector>
using namespace std;
void remove_char(char* str, char ch) {
char* p = str;
while (*p ) {
if (*p != ch) {
*str++ = *p;
}
p++;
}
*str = '\0';
}
int main() {
char str[] = "hello world";
remove_char(str, 'l');
printf("Modified string = %s\n", str);
return 0;
}
七、题目: 编写一个函数,实现字符串复制功能,即将一个字符串复制到另一个字符数组中。
#include <iostream>
#include <vector>
using namespace std;
void my_strcpy(char* dest, const char* src) {
while (*src != '\0') {
*dest++ = *src++;
}
*dest = '\0';
}
int main() {
char src[] = "Hello, World!";
char dest[50];
my_strcpy(dest, src);
printf("Copied string = %s\n", dest);
return 0;
}
八、题目: 编写一个函数,判断一个字符串是否是另一个字符串的子串,并返回子串的起始位置。
#include <iostream>
#include <string.h>
using namespace std;
const char* find_substring(const char* str, const char* sub) {
const char* s1 = str;
const char* s2 = 0;
const char* p = str;
while (*p) {
s2 = sub;
p = s1;
while (*s1&&*s1!=*s2) {
p++; s1++;
}
while (*s1 && *s2 && *s1 == *s2) {
s1++; s2++;
}
if (*s2 == '\0') {
return p;
}
}
return NULL;
}
int main() {
const char* str = "Hello, World!";
const char* sub = "World";
const char* pos = find_substring(str, sub);
if (pos) {
printf("Substring found at index: %ld\n", pos - str);
}
else {
printf("Substring not found.\n");
}
return 0;
}
九、题目: 给定一个单链表,编写一个函数逆序输出链表中的所有元素。
#include <iostream>
#include <string.h>
using namespace std;
typedef struct Node {
int data;
struct Node* next;
} Node;
void print_reverse(Node* head) {
Node* next = NULL;
Node* prev = NULL;
while (head) {
next = head->next;
head->next = prev;
prev = head;
head = next;
}
while (prev != NULL) //如果p不为空,说明链表中有元素
{
cout << prev->data << endl; //输出表中元素
prev = prev->next; // P指向下一个结点,直到p为空
}
}
int main() {
Node* head = (Node*)malloc(sizeof(Node));
head->data = 1;
head->next = (Node*)malloc(sizeof(Node));
head->next->data = 2;
head->next->next = (Node*)malloc(sizeof(Node));
head->next->next->data = 3;
head->next->next->next = NULL;
print_reverse(head);
printf("\n");
// Free memory
Node* temp;
while (head) {
temp = head;
head = head->next;
free(temp);
}
return 0;
}
十、题目: 编写一个程序,动态分配一个整型数组,并释放这块内存空间(直接写在main里)。
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
int* p = (int*)malloc(sizeof(int));
free(p);
return 0;
}
十一、题目: 编写一个函数,计算整型数组中所有元素的平均值,并通过指针返回结果。
#include <iostream>
using namespace std;
double array_average(int* arr, int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum = sum + *arr;
arr++;
}
return sum / size;
}
int main() {
int arr[] = { 2, 4, 6, 8, 10 };
int size = sizeof(arr) / sizeof(arr[0]);
double avg = array_average(arr, size);
printf("Average = %.2f\n", avg);
return 0;
}
十二、题目: 编写一个函数,统计整型数组中指定值出现的次数,并通过指针返回结果。
#include <iostream>
using namespace std;
int count_occurrences(int* arr, int size, int target) {
int num = 0;
for (int i = 0; i < size; i++) {
if (*arr == target) {
num++;
}
arr++;
}
return num;
}
int main() {
int arr[] = { 1, 2, 3, 2, 2, 4, 5, 2 };
int size = sizeof(arr) / sizeof(arr[0]);
int target = 2;
int occurrences = count_occurrences(arr, size, target);
printf("Occurrences of %d = %d\n", target, occurrences);
return 0;
}
十三、题目: 编写一个函数,交换输入字符串中的两个单词,并通过指针修改原始字符串
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void swap_words(char* str)
{
char* w1 = (char*)malloc(strlen(str) + 1);
char* w2 = (char*)malloc(strlen(str) + 1);
char* headstr = str;
char* headw1 = w1;
char* headw2 = w2;
while (*str != ' ') {
*w1++ = *str++;
}
str++;
*w1 = '\0';
while (*str != '\0') {
*w2++ = *str++;
}
*w2++ = ' ';
*w2 = '\0';
while (*headw2) {
*headstr++ = *headw2++;
}
while (*headw1) {
*headstr++ = *headw1++;
}
}
int main() {
char str[] = "Hello World";
swap_words(str);
printf("Swapped string = %s\n", str);
return 0;
}
十四、题目: 编写一个函数,计算输入字符串的长度,并通过指针返回结果。
#include <iostream>
using namespace std;
int string_length(const char* str) {
int n = 0;
while (*str!='\0') {
n++;
str++;
}
return n;
}
int main() {
const char* str = "Hello";
int length = string_length(str);
printf("Length of string '%s' = %d\n", str, length);
return 0;
}
十五、题目: 编写一个函数,将输入字符串中的所有小写字母转换为大写,并通过指针修改原始字符串。
#include <iostream>
using namespace std;
void to_uppercase(char* str) {
while (*str != '\0') {
while (*str != ' ' && *str != '\0') {
*str=toupper(*str);
str++;
}
if (*str == ' ') {
str++;
}
}
}
int main() {
char str[] = "hello World";
to_uppercase(str);
printf("Uppercase string = %s\n", str);
return 0;
}
十六、题目: 编写一个函数,判断输入字符串是否是回文,并通过指针返回结果(1表示是,0表示否)。
#include <iostream>
#include <string.h>
using namespace std;
int is_palindrome(char* str) {
int len = strlen(str);
int i = 0;
while (*str != '\0') {
while (str[i] == str[len - 1]&&(i<len-1)) {
i++; len--;
}
if (i >= len - 1) {
return 1;
}
else {
return 0;
}
}
}
int main() {
char str[] = "madam";
if (is_palindrome(str)) {
printf("%s is a palindrome.\n", str);
}
else {
printf("%s is not a palindrome.\n", str);
}
return 0;
}
十七、题目: 编写一个函数,交换二维整型数组中指定的两行或两列,并通过指针修改原始数组。
#include <iostream>
using namespace std;
void swap_rows(int arr[][3], int row1, int row2, int cols) {
int tmp[3][3];
for (int i = 0; i < cols; i++) {
**tmp = arr[row1][i];
arr[row1][i] = arr[row2][i];
arr[row2][i] = **tmp;
}
}
int main() {
int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int rows = 3, cols = 3;
int row1 = 0, row2 = 2;
swap_rows(arr, row1, row2, cols);
// Print modified array
printf("Modified array after swapping rows:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
十八、题目: 编写一个函数,按指定格式打印二维整型数组的内容。
#include <iostream>
using namespace std;
void print_matrix(int arr[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main() {
int arr[2][3] = { {1, 2, 3}, {4, 5, 6} };
int rows = 2, cols = 3;
printf("Matrix elements:\n");
print_matrix(arr, rows, cols);
return 0;
}
十九、题目: 编写一个程序,动态分配一个二维整型数组,并释放这块内存空间(直接写在main里)。
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
int m = 0; int n = 0;
int** arr = (int**)malloc(m * sizeof(int*));
for (int i = 0; i < m; i++)
{
arr[i] = (int*)malloc(n * sizeof(int));
}
for (int i = 0; i < m; i++)
free(arr[i]);/*释放列*/
free(arr);/*释放行*/
return 0;
}
二十、题目: 编写一个函数,将一个一维整型数组复制到另一个数组,并通过指针修改原始数组。
#include <iostream>
using namespace std;
void copy_array(int* src, int* dest, int size) {
for (int i = 0; i < size; i++) {
*dest++ = *src++;
}
}
int main() {
int src[] = { 1, 2, 3, 4, 5 };
int dest[5];
int size = sizeof(src) / sizeof(src[0]);
copy_array(src, dest, size);
printf("Source array: ");
for (int i = 0; i < size; i++) {
printf("%d ", src[i]);
}
printf("\n");
printf("Copied array: ");
for (int i = 0; i < size; i++) {
printf("%d ", dest[i]);
}
printf("\n");
return 0;
}