交换两个整数的值 题目: 编写一个函数,使用指针交换两个整数变量的值。
# include <stdio.h>
void swap ( int * a, int * b) {
int temp = * a;
* a = * b;
* b = temp;
}
int main ( ) {
int x = 10 , y = 20 ;
swap ( & x, & y) ;
printf ( "x = %d, y = %d\n" , x, y) ;
return 0 ;
}
计算数组元素的和 题目: 编写一个函数,计算整型数组中所有元素的总和,并通过指针返回结果。
# include <stdio.h>
void array_sum ( int * arr, int size, int * sum) {
* sum = 0 ;
for ( int i = 0 ; i < size; i++ ) {
* 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 <stdio.h>
void find_max ( int * arr, int size, int * max_value, int * index) {
* max_value = arr[ 0 ] ;
* index = 0 ;
for ( int i = 1 ; 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 <stdio.h>
# include <string.h>
void reverse_string ( char * str) {
int len = strlen ( str) ;
char * start = str;
char * end = str + len - 1 ;
char temp;
while ( start < end) {
temp = * start;
* start = * end;
* end = temp;
start++ ;
end-- ;
}
}
int main ( ) {
char str[ ] = "hello" ;
reverse_string ( str) ;
printf ( "Reversed string = %s\n" , str) ;
return 0 ;
}
统计字符串中的单词个数 题目: 编写一个函数,统计输入字符串中的单词个数,并通过指针返回结果。
# include <stdio.h>
# include <ctype.h>
int count_words ( const char * str) {
int count = 0 ;
int in_word = 0 ;
while ( * str) {
if ( isspace ( * str) ) {
in_word = 0 ;
} else if ( ! in_word) {
in_word = 1 ;
count++ ;
}
str++ ;
}
return count;
}
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 <stdio.h>
void remove_char ( char * str, char ch) {
char * p1 = str, * p2 = str;
while ( * p2) {
if ( * p2 != ch) {
* p1++ = * p2;
}
p2++ ;
}
* p1 = '\0' ;
}
int main ( ) {
char str[ ] = "hello world" ;
remove_char ( str, 'l' ) ;
printf ( "Modified string = %s\n" , str) ;
return 0 ;
}
实现strcpy函数 题目: 编写一个函数,实现字符串复制功能,即将一个字符串复制到另一个字符数组中。
# include <stdio.h>
void my_strcpy ( char * dest, const char * src) {
while ( ( * dest++ = * src++ ) ) {
}
}
int main ( ) {
char src[ ] = "Hello, World!" ;
char dest[ 50 ] ;
my_strcpy ( dest, src) ;
printf ( "Copied string = %s\n" , dest) ;
return 0 ;
}
查找子字符串 题目: 编写一个函数,判断一个字符串是否是另一个字符串的子串,并返回子串的起始位置。
# include <stdio.h>
# include <string.h>
const char * find_substring ( const char * str, const char * sub) {
return strstr ( str, sub) ;
}
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 <stdio.h>
# include <stdlib.h>
typedef struct Node {
int data;
struct Node * next;
} Node;
void print_reverse ( Node * head) {
if ( head == NULL ) return ;
print_reverse ( head-> next) ;
printf ( "%d " , head-> data) ;
}
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" ) ;
Node * temp;
while ( head) {
temp = head;
head = head-> next;
free ( temp) ;
}
return 0 ;
}
动态内存分配和释放 题目: 编写一个程序,动态分配一个整型数组,并释放这块内存空间。
# include <stdio.h>
# include <stdlib.h>
int main ( ) {
int size = 5 ;
int * arr = ( int * ) malloc ( size * sizeof ( int ) ) ;
if ( arr == NULL ) {
printf ( "Memory allocation failed\n" ) ;
return 1 ;
}
for ( int i = 0 ; i < size; i++ ) {
arr[ i] = i * 2 ;
printf ( "%d " , arr[ i] ) ;
}
printf ( "\n" ) ;
free ( arr) ;
return 0 ;
}
求解平均值 题目: 编写一个函数,计算整型数组中所有元素的平均值,并通过指针返回结果。
# include <stdio.h>
double array_average ( int * arr, int size) {
int sum = 0 ;
for ( int i = 0 ; i < size; i++ ) {
sum += arr[ i] ;
}
return ( double ) 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 <stdio.h>
int count_occurrences ( int * arr, int size, int target) {
int count = 0 ;
for ( int i = 0 ; i < size; i++ ) {
if ( arr[ i] == target) {
count++ ;
}
}
return count;
}
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>
void swap_words ( char * str) {
char * space = strchr ( str, ' ' ) ;
if ( space) {
* space = '\0' ;
char * second_word = space + 1 ;
printf ( "%s %s\n" , second_word, str) ;
strcat ( second_word, " " ) ;
strcat ( second_word, str) ;
strcpy ( str, second_word) ;
}
}
int main ( ) {
char str[ ] = "Hello World" ;
swap_words ( str) ;
printf ( "Swapped string = %s\n" , str) ;
return 0 ;
}
字符串长度 题目: 编写一个函数,计算输入字符串的长度,并通过指针返回结果。
# include <stdio.h>
int string_length ( const char * str) {
int length = 0 ;
while ( * str != '\0' ) {
length++ ;
str++ ;
}
return length;
}
int main ( ) {
const char * str = "Hello" ;
int length = string_length ( str) ;
printf ( "Length of string '%s' = %d\n" , str, length) ;
return 0 ;
}
将字符串转换为大写 题目: 编写一个函数,将输入字符串中的所有小写字母转换为大写,并通过指针修改原始字符串。
# include <stdio.h>
# include <ctype.h>
void to_uppercase ( char * str) {
while ( * str) {
* str = toupper ( * str) ;
str++ ;
}
}
int main ( ) {
char str[ ] = "hello World" ;
to_uppercase ( str) ;
printf ( "Uppercase string = %s\n" , str) ;
return 0 ;
}
检查字符串是否回文 题目: 编写一个函数,判断输入字符串是否是回文,并通过指针返回结果(1表示是,0表示否)。
# include <stdio.h>
# include <string.h>
int is_palindrome ( char * str) {
int len = strlen ( str) ;
char * start = str;
char * end = str + len - 1 ;
while ( start < end) {
if ( * start != * end) {
return 0 ;
}
start++ ;
end-- ;
}
return 1 ;
}
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 <stdio.h>
void swap_rows ( int arr[ ] [ 3 ] , int row1, int row2, int cols) {
for ( int i = 0 ; i < cols; i++ ) {
int temp = arr[ row1] [ i] ;
arr[ row1] [ i] = arr[ row2] [ i] ;
arr[ row2] [ i] = temp;
}
}
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) ;
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 <stdio.h>
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 ( "%3d " , 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 ;
}
动态分配二维数组 题目: 编写一个程序,动态分配一个二维整型数组,并释放这块内存空间。
# include <stdio.h>
# include <stdlib.h>
int main ( ) {
int rows = 3 , cols = 4 ;
int * * matrix = ( int * * ) malloc ( rows * sizeof ( int * ) ) ;
for ( int i = 0 ; i < rows; i++ ) {
matrix[ i] = ( int * ) malloc ( cols * sizeof ( int ) ) ;
}
for ( int i = 0 ; i < rows; i++ ) {
for ( int j = 0 ; j < cols; j++ ) {
matrix[ i] [ j] = i * cols + j;
printf ( "%3d " , matrix[ i] [ j] ) ;
}
printf ( "\n" ) ;
}
for ( int i = 0 ; i < rows; i++ ) {
free ( matrix[ i] ) ;
}
free ( matrix) ;
return 0 ;
}
复制一维数组到另一个数组 题目: 编写一个函数,将一个一维整型数组复制到另一个数组,并通过指针修改原始数组。
# include <stdio.h>
void copy_array ( int * src, int * dest, int size) {
for ( int i = 0 ; i < size; i++ ) {
dest[ i] = src[ i] ;
}
}
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 ;
}